What is the purpose of constructors in java ? creating object or initializing instance variables?
Constructor is used to create object
Constructor is used to initialize the instance variables of an object.
It is advisable to use constructor to initialize static variables also.
Constructor will be called before creating the object.
Constructor is used to only initialize the object, that is initialize the instance variables of an object. It is not a good practice to initialize static variables in a constructor. since constructors are used to initialize objects, so it will be called only after creating objects. it is not the constructor but new operator that will create the object in java.
Note : It is new operator who creates object, not constructor. Constructors are used to initialize instance variables once object is created.
Back To Top