Reading time : 5 minutes
Previous Chapter >>>> Classes and Objects in Java
Previous Chapter >>>> Classes and Objects in Java
Constructor methods are named exactly as the class name.
Unlike normal methods, class constructors don’t need to a return a specific datatype or a value.
The Compiler considers a default constructor for a class having no constructor defined explicitly.
Why are Constructors required?
In case exposing class variables to the main program is not secure, class variables can be declared as private i.e. inaccessible from other classes. By defining Constructors main function would access the constructor instead of accessing class variables.
Sample program illustrating use of Class Constructors in Java:
Sample program illustrating use of Class Constructors in Java:
Basic Java for Testers | Ch-7 | Class Constructors in Java
Reviewed by Ishan Dev Shukl
on
March 27, 2018
Rating:

No comments:
Thanks a lot for your valuable Comment!