Constructor in JAVA

In this article, we will go through Java constructors, their types with the help of examples.

What is a Constructor?

Constructor is a special member function in JAVA whose name is the same as the class name. It is used to initialize objects at the time of their creation.

Example 1: Java constructor

class DemoExample {
    DemoExample() {
        // constructor body
    }
}

Here, DemoExample() is a constructor. It has the same name as that of the class and does not have any return type, not even void.

Constructor in JAVA

It has no return type, not even void.

It is invoked at the time of object creation or when an instance is created.


Types of Constructor

  • No-Arguments Constructor
  • Parameterized Constructor
  • Default Constructor

No-Arguments Constructor

  • The no-argument constructor is a constructor that does not accept any parameters/arguments.

Example 2: Java No-argument constructor

class Rectangle {
    double length, width;

    //No-argument constructor
    Rectangle() {
        setDetails(52, 47);
    }

    void setDetails(double l, double w) {
        length = l;
        width = w;
    }

    void display() {
        System.out.println("-----------------------");
        System.out.println("Length: " + length);
        System.out.println("Width: " + width);
        System.out.println("Area: " + calcArea());
    }

    double calcArea() {
        return length * width;
    }
}

class ConstructorDemo1 {
    public static void main(String[] args) {
        Rectangle r1 = new Rectangle();
        r1.display();
    }
}

Example 2 Output

-----------------------
Length: 52.0
Width: 47.0
Area: 2444.0

Parameterized Constructor

  • The Java parameterized constructor can also accept one or more parameters/arguments.
  • Such constructors are known as parameterized constructors or constructors with parameters.

Example 3: Java Parameterized Constructor

class Rectangle {
    double length, width;

    //Parameterized constructor
    Rectangle(double l, double w) {
        setDetails(l, w);
    }

    void setDetails(double l, double w) {
        length = l;
        width = w;
    }

    void display() {
        System.out.println("-----------------------");
        System.out.println("Length: " + length);
        System.out.println("Width: " + width);
        System.out.println("Area: " + calcArea());
    }

    double calcArea() {
        return length * width;
    }
}

class ConstructorDemo2 {
    public static void main(String[] args) {
        Rectangle r1 = new Rectangle(101, 70);
        r1.display();
    }
}

Example 3 Output

-----------------------
Length: 101.0
Width: 70.0
Area: 7070.0

Default Constructor

  • The Java compiler will create a no-argument constructor automatically when the user-defined constructor is not explicitly declared. Such constructor is known as the default constructor.
  • Each class has its own default constructor.
  • The Default constructor do not accept arguments.
  • Constructor declared by compiler initializes the object with default values.
  • Once the user defines its own default constructor, the compiler will use it to initialize an object.

Example 4: Java Default Constructor

class Rectangle {
    double length, width;

    void display() {
        System.out.println("-----------------------");
        System.out.println("Length: " + length);
        System.out.println("Width: " + width);
    }
}

class ConstructorDemo3 {
    public static void main(String[] args) {
        Rectangle r1 = new Rectangle();
        r1.display();
    }
}

Example 4 Output

-----------------------
Length: 0.0
Width: 0.0

Any uninitialized instance variables will be initialized with default values by the default constructor.

TypeDefault Value
booleanfalse
byte0
short0
int0
long0L
char\u0000
float0.0f
double0.0d
objectReference null

Summary

  • Constructors are invoked implicitly when an object is instantiated.
  • The name of the constructor must be the same as the class & Java constructor must not have a return type.
  • If a class does not contain any declaration of the explicit constructor, the Java compiler automatically creates a default constructor during run-time.
  • Types of Constructors :
    • No-Argument Constructor:- A constructor that does not accept any arguments/parameters
    • Parameterized constructor:- A constructor that accepts one or more arguments/parameters
    • Default Constructor:- The Java compiler automatically creates a constructor if it is not explicitly defined.
  • A constructor cannot be static, abstract or final.

Hope you like this!

Keep helping and happy 😄 coding

Avatar

A web geek, an industry experienced web developer & tutor/instructor residing in India 🇮🇳

Next
Previous

Related