In Java Constructors are used to set initial values for variables. The compiler calls the constructor first when an object is created. A Constructor is called automatically at run-time when the compiler creates a new object.
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: