You are currently viewing Java Constructor

Java Constructor

  • Post author:
  • Post category:Java
  • Post comments:0 Comments
  • Post last modified:August 12, 2020
  • Reading time:3 mins read

A Java Constructor is a method with the same name as the class. It is called when we initialize any class in java and it has no return type.

What actually happens is when we try to create an instance of any class we do something like this ⇒ Car car = new Car();

When we write new Car()  that is the time when the constructor is called.

Syntax

So how does a constructor looks like?

class Car{
    Car() {
         // Do something here
    }
}

This is an example of a constructor. Whenever we create an instance of this Car class, that constructor is called first.

If we do not define any constructor then the java compiler provides a default constructor to create the instance.

Now, what if we wish to pass data to that constructor method? How can we achieve that?

There are two kinds of constructors,

  1. No Argument Constructor(Default Constructor)
  2. Parameterized Constructor

Default Constructor

Suppose we have created a class and declared some other methods and kinds of stuff but no java constructor in it. If we want to access those properties of the class then we have to initialize it. Below is out class

class Car{ 
    public void drive(){
        System.out.println("Drive the car")
    }
}
// now we can simply access like this
Car mCar = new Car();
mCar.drive();

Above we can see that there is no constructor is declared but we can access the method in it as we have initialized with the default constructor provided by the compiler.

The default constructor is used to provide the default values to the object like 0, null, etc., depending on the type.

Parameterized Constructor

A parameterized constructor function has some parameters that have to provide during creating the instance of the object.

class Car {
    Car(String model, String color) { 
        // Do something here 
    }
}

Now if we want to create an instance of that car class we have to write like

Car mCar = new Car("BMW","Black");

Constructor Overloading

We can create as much Java Constructor we need. This is Constructor Overloading

package com.car;

public class Car {
    private String name;
    private String color;
    private int wheel;

    public Car() {
    }

    public Car(String name, String color) {
        this.name = name;
        this.color = color;
    }

    public Car(String name, String color, int wheel) {
        this.name = name;
        this.color = color;
        this.wheel = wheel;
    }

    void display(){
        System.out.println(name+" "+color+" "+wheel);
    };

    public static void main(String args[]){
        Car car1 = new Car("BMW","Red");
        Car car2 = new Car("GT","Gray",4);
        car1.display();
        car2.display();
    }

}

Output

BMW Red 0
GT Gray 4

So, now we have some knowledge about the constructor. We also can use wikipedia. let’s dive in and stay put with AndroVaid.

Leave a Reply