Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Constructor

A constructor is very similar to method.
It has a body like method.
It can have parameters like method.
The name of constructor & its class is same.
It does not have any return type. Not even void.

What is the use of constructor?

A constructor is used for initializing an object immediately after the creation of an object.
Many of us can feel that constructor is used to create an object, but it is not true. A Constructor is mainly used to initialize an object.
An object can be initialized using method also. But it is better & simple to initialize an object using constructor.

Example

public class Car 
{
    String name;
    String color;
    int gears;
    boolean isAutoGear;
	
    Car()   // Constructor
    {
        name = "BMW-520";
        color = "Black";
        gears = 6;
        isAutoGear = true;
    }

    public static void main(String args[])
    {
        Car c = new Car();
        System.out.println(c.name);
        System.out.println(c.color);
        System.out.println(c.gears);
        System.out.println(c.isAutoGear);
    }
}

Output

BMW-520
Black
6
True

If we didn’t define a constructor explicitly, Java provides its own constructor called default constructor.
Java uses default constructor to initialize all the instance variables to their default values, automatically.
Numeric type variables to zero, reference type variables to null & boolean type variables to false.

Example

public class Car 
{
    String name;
    String color;
    int gears;
    boolean isAutoGear;
	
    public static void main(String args[])
    {
        Car c = new Car();
        System.out.println(c.name);
        System.out.println(c.color);
        System.out.println(c.gears);
        System.out.println(c.isAutoGear);
    }
}

Output

null
null
0
false

Parameterized Constructor

Earlier we have used constructor without parameters called the parameterless constructor.
But if there are two or more objects of the same class, every object will invoke the same parameterless constructor, and each object will contain same values for their variables.

Example

public class Car 
{
    String name;
    String color;
    int gears;
    boolean isAutoGear;
	
    Car()     // Parameter-less constructor
    {
        name = "BMW-520";
        color = "Black";
        gears = 6;
        isAutoGear = true;
    }
	
    public void display()
    {
        System.out.println(name);
        System.out.println(color);
        System.out.println(gears);
        System.out.println(isAutoGear);
        System.out.println("----------------");
    }

    public static void main(String args[])
    {
        Car c1 = new Car();
        Car c2 = new Car();
        Car c3 = new Car();
        c1.display();
        c2.display();
        c3.display();		
    }
}

Output

BMW-520
Black
6
true
----------------
BMW-520
Black
6
true
----------------
BMW-520
Black
6
true
----------------

Here we can see that each variable of a Car object has same specifications.
But what if we want each Car object to be initialized with different specifications using the same constructor?
The only way to achieve such condition is to use parameterized constructor.
Parameterized constructor contains one or more parameters to receive the values of same or different data types sent to it at the time of object declaration.
The way of using parameter list in constructor is exactly same as the parameter list in method.

Example of parameterized constructor

public class Car 
{
    String name;
    String color;
    int gears;
    boolean isAutoGear;
		
    Car(String n,String c,int g,boolean ag)   // Parameterized constructor
    {
        name = n;
        color = c;
        gears = g;
        isAutoGear = ag;
    }
		
    public void display()
    {
        System.out.println(name);
        System.out.println(color);
        System.out.println(gears);
        System.out.println(isAutoGear);
        System.out.println("----------------");
    }

    public static void main(String args[])
    {
        Car c1 = new Car("BMW-520","Black",6,true);
        Car c2 = new Car("Ferrari","Red",7,true);
        Car c3 = new Car("Mercedes","White",6,false);
        c1.display();
        c2.display();
        c3.display();		
    }
}

Output

BMW-520
Black
6
true
----------------
Ferrari
Red
7
true
----------------
Mercedez
White
6
false
----------------

Now we can see that in the above program same constructor is used to set different specifications for each object of a Car at the time of declaration.
This is possible only because of the parameterized constructor.

Java Programming Language Parameterized Constructor

Leave a reply