What is a constructor?

A constructor is a special and optional method in a class that gets invoked each time when an object of the class is created. The main purpose of a constructor is to initialize an object with some default values at the time of object creation.

The constructor is exactly the same as any other member function, but it has some special properties and also you need to follow some rules to create a constructor.

Rules to create a constructor

Following are a few rules to create a constructor –

  1. The constructor name must exactly match the class name.
  2. Constructors should not have an explicit return type.
  3. Java constructors cannot be abstract, static, final, and synchronized.

Types of constructors

There are three different types/ways to create a constructor in Java – 

  1. Default constructor
  2. No-arg constructor
  3. Parameterized constructor

Default constructor

If the class does not have a user-defined constructor, the compiler simply adds one default no-arg constructor and initializes member variables to default values. See the following example of a class where no user-defined constructor is created.

Example

public class Example {

    // No user-defined constructor

    public static void Example(String[] args) {

        // Create a object of Example
        Example example = new Example();

    }
}

Implementing a constructor will prevent it from receiving a default constructor from the Java compiler.

No-arg constructor

A constructor with no parameters is called a no-arg or default constructor. The constructor with no arguments has the same signature as the default constructor, but unlike the default constructor, where the body of the constructor is empty, you can include code in the body of the constructor without arguments.

Syntax

The syntax for a no-arg constructor.

<modifier> <class-name>() {
    // Constructor body
}

Example

Following is the example to create a no-arg constructor.

public class Example {

    // No-arg constructor    
    public Example() {
        System.out.println("Inside constructor!");
    }

    public static void main(String[] args) {

        // Create a object of Example
        Example example = new Example();

    }
}

Parameterized constructor

A constructor with N number of the parameter is known as a parameterized constructor.

Unlike no-arg constructors, you can create parameterized constructors to accept N parameters that can be used to initialize data members.

Syntax

The syntax for a parameterized constructor.

<modifier> <class-name>(<parameter list>) {
    // Constructor body
}

Example

Following is the example to create a parameterized constructor.

public class Fruit {

    String name;
    String color;
    double size;
    double price;

    public Fruit(String fName, String fColor, double fSize, double fPrice) {
        name = fName;
        color = fColor;
        size = fSize;
        price = fPrice;

        System.out.println("Data member initialized!");
    }

    public static void main(String[] args) {

        // Apple
        Fruit apple = new Fruit("Apple", "RED", 8.0, 150);

    }
}

Points to remember

  1. A class can have both no-arg and parameterized constructor.
  2. A parameterized constructor can also have overloaded constructor just like other member functions.
  3. A constructor can optionally have one of (private, protected, public) access-modifiers. Otherwise, the default modifier is used.
  4. A constructor always returns an instance of its class.
  5. Constructors can be overridden by child classes just like any other member functions.