Constructor in Java

prepare1 min read

A Java constructor is used to initialize an object when it is created. A constructor always have the same name as that of a class.

Syntax:

constructor name{

// Initialization code

}

A constructor has no return type. But it implicitly return the instance of the class type. Whenever an object is created, the JVM will look for a constructor defined in the class. But when, there is no constructor defined by the programmer, it calls the default constructor. The default constructor initialize the variables with default values.