1. Home
  2. Java
  3. Java OOPs
  4. Constructors

Constructors

A constructor is a special method created with the same name as the class name and no return type. It is used to initialize a class object.

If no constructor defined in the class, Java called a default constructor to initialize an object with default values. For example, any int variable initializes with 0 and object initialized with null.

Rules for Constructors:

  • Any constructor(s) of a class must have the same name as the class name in which it resides.
  • A constructor in Java cannot be static, abstract, final, and Synchronized.
  • A constructor has no return type even void.

Example of Constructor in Java