0

Why we need constructer if we have methods in java . Ex-method like putData

21st Jan 2020, 3:23 PM
Soham
2 odpowiedzi
+ 5
Constructors allow us to directly initialize objects upon declaration, and is on most occasions more semantically fit to be used instead of calling setters after declaring the object. https://stackoverflow.com/questions/19359548/setter-methods-or-constructors/42798426
21st Jan 2020, 3:46 PM
Hatsy Rei
Hatsy Rei - avatar
+ 2
Constructor itself is a method but without a return type. Whenever you create an instance of a class then you do nothing but make a call to the constructor of the specific class to say that an object of the class has been created. Object obj = new Object(); Even if you do not create a constructor, the compiler by default will create a no argument constructor for the class.
21st Jan 2020, 3:44 PM
Avinesh
Avinesh - avatar