Please what's the main idea behind constructors??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Please what's the main idea behind constructors???

for instance when someone is using a constructor, what's the programmer trying to do?

18th Oct 2021, 6:40 PM
Mbeng Clovert
Mbeng Clovert - avatar
4 Answers
+ 1
Constructors are usually used to initialize instance variables
18th Oct 2021, 7:19 PM
Adithya Keshav T
Adithya Keshav T - avatar
+ 1
Constructors are specific to object-oriented programming languages. Programs written in these languages consist of classes, which define sets of properties (data) and behaviours (methods). These are called the class’s members. An instance of a class is an object created from its class definition. It obtains a copy of the defined members (I’m leaving out some stuff here for simplicity) and occupies physical space in memory. The process of creating an object this way is called instantiation. Constructors are methods found (in one way or another) in every class. In order for instantiation to occur, one of the class’s constructors must be invoked to construct the object instance and initialize its data members to known values. After this is completed, the object can be used by accessing its exposed data and invoking its methods as needed. But a constructor can only be used once in an object’s lifetime: to create that particular instance of its class.
19th Oct 2021, 7:02 AM
Arun Jamson
Arun Jamson - avatar
0
You use it when you want to initialize an object
18th Oct 2021, 7:36 PM
Jasy Fabiano
Jasy Fabiano - avatar
0
Constructor is used to create objects It is called sutomatically while creating object Since constructor is non static we can call both static and non static methods We can perform overloading and can call superclass constructor very easily using super() We can perform chaining of cobstructors using this()
19th Oct 2021, 7:19 AM
sree harsha
sree harsha - avatar