Constructor

Author: Ravi Poswal

A constructor is a special class member used to allocate the memory of the class. If a class does not contain any constructor, the compiler provides the default constructor at the compilation time. You can check using javap command. It has some following features:-   

  • It has the same name as its class name.

  • It does not have any explicit return type.

  • It can’t be abstract, static, final, and synchronized.

The constructor initializes the data member of the object. There are two different types of constructors in java.

  • Default constructor

  • Parameterized constructor

Code Example

class A 
{
    public static void main(String arg[])
         { System.out.println("hello");
          } 
}