+ 4
What is constructor ?
Answer
5 Antworten
+ 3
Constructor is a function with same as class name and do NOT have any return type and it will call when instance of that class is created.
Types of cinstructor
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
+ 16
Constructor is class member function and it's name is same as class name. It is used to initialize values of variables and called automatically when object of class is created.
+ 3
Please use the search bar. This question has been asked many many times.
Also you could look into the java course here on sololearn.
Or google for it. You will find many answers to it.
https://www.sololearn.com/discuss/1780152/?ref=app
https://www.sololearn.com/discuss/1800718/?ref=app
https://www.sololearn.com/discuss/1662406/?ref=app
https://www.sololearn.com/discuss/1749899/?ref=app
https://www.sololearn.com/discuss/1755114/?ref=app
https://www.sololearn.com/discuss/1801634/?ref=app
https://www.sololearn.com/discuss/1807425/?ref=app
https://www.sololearn.com/discuss/1787095/?ref=app
https://www.sololearn.com/discuss/1653813/?ref=app
https://www.sololearn.com/discuss/1644667/?ref=app
+ 3
Constructor is a function with same as class name and do NOT have any return type , it call implicitly and in special cases it also call explicitly
+ 2
Constructor can be used to initialize dafault values, establishing connections, allocate memory, allocating resources etc. Can be done when object is created.
So, we can put that code in constructor.