Constructor in JAVA
Tags: java, constructor
Published: 2021-11-27 09:37:17

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

```java
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

```java
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

```text
-----------------------
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

```java
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

```text
-----------------------
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

```java
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

```text
-----------------------
Length: 0.0
Width: 0.0
```

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

| Type    | Default Value    |
| ------- | ---------------- |
| boolean | `false`          |
| byte    | `0`              |
| short   | `0`              |
| int     | `0`              |
| long    | `0L`             |
| char    | `\u0000`         |
| float   | `0.0f`           |
| double  | `0.0d`           |
| object  | `Reference null` |


Read the complete post on our site [Constructor in JAVA
](https://meshworld.in/java-constructor/)

Read others posts on our site [MeshWorld](https://meshworld.in)

Hope you like this!

Keep helping and happy 😄 coding