Parameterized Constructor in Java with Example

A Constructor which has parameters in it called as Parameterized Constructors, this constructor is used to assign different values for the different objects.  In the below example we have a constructor for the Car class which takes in the the value and sets to the property, lets try to set the value for the property “carColor”

public class Car 
{
    String carColor;
    Car(String carColor)
    {
        this.carColor = carColor;
    }
    
    public void disp()
    {
        System.out.println("Color of the Car is : "+carColor);
    }
    public static void main(String args[])
    {
        //Calling the parameterized constructor
        Car c = new Car("Blue");
        c.disp();
    }
}

Output :

Color of the Car is : Blue

Can we have both Default Constructor and Parameterized Constructor in the same class?

Yes, you have both Default Constructor  and Parameterized Constructor in the same class.

public class Car 
{
    String carColor;
    Car()
    {
        System.out.println("No argument Constructor of Car class called");
    }
    
    Car(String carColor)
    {
        this.carColor = carColor;
    }
    
    public void disp()
    {
        System.out.println("Color of the Car is : "+carColor);
    }
    public static void main(String args[])
    {
        //Calling the No argument constructor
        Car c1 = new Car();
        
        //Calling the parameterized constructor
        Car c2 = new Car("Blue");
        c2.disp();
    }
}

Output :

No argument Constructor of Car class called
Color of the Car is : Blue

Can i have a class with No Constructor ? What will happen during object creation ? 

Yes, we can have a class with no constructor, during the time of object creation the compiler will create a default constructor for you automatically.

public class Car 
{
    public void disp()
    {
        System.out.println("disp() method of the Car class called");
    }
    public static void main(String args[])
    {
        //Calling the No argument constructor
        Car c1 = new Car();
        
        c1.disp();
    }
}

Output :

disp() method of the Car class called

In the above code we haven’t declared the default constructor, yet we have created a object and called the disp() method over it. This is possible only because the compiler has created the default constructor for you.

Does compiler create Default constructor everytime ?

No, The compiler will not create Default Constructor when you have already defined a constructor. Lets take a look into the below example.

public class Car 
{
    String carColor;
    Car(String carColor)
    {
        this.carColor = carColor;
    }
    
    public void disp()
    {
        System.out.println("Color of the Car is : "+carColor);
    }
    public static void main(String args[])
    {
        //Calling the No argument constructor
        Car c1 = new Car();
        
        //Calling the parameterized constructor
        Car c2 = new Car("Blue");
        c2.disp();
    }
}

Output :

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The constructor Car() is undefined

	at com.javainterviewpoint.Car.main(Car.java:18)

When we run the above code we will get the above exception thrown, the reason is when you haven’t declared any constructor in your class the compiler will create one for you. But here we already have a constructor which take parameters and hence the compiler will not create the default one for you. So we will be getting the compilation error.

Leave a Reply

Your email address will not be published. Required fields are marked *