What is Parameterized Constructors in java?

Let's first understand about Constructors in brief.

Constructors Constructors is used to initialized class attributes while creating object using new keyword.

There is multiple types of Constructors as below

  1. Default Constructors (No argument Constructors)
  2. Parameterized Constructors
  3. Copy Constructors

Here we are leaning about Parameterized constructor. Parameterized is defined to initialized class params with initial values.

Example : Parameterized constructor JavaApplication below set class param year with values assigned while creating object. Please refer following example.

public JavaApplication(int year) { 

        this.year = year;

    }

Refer below Leap year program for demonstration


public class JavaApplication {


    int year;

    public JavaApplication(int year) { // Parameterized constructor

        this.year = year;

    }

    public boolean isLeapYear() {

        if (this.year % 4 == 0) {

            // check if year divided by 100 for century test
            if (this.year % 100 == 0) {


                if (this.year % 400 == 0) {
                    return true;
                } else {
                    return false;
                }
            } 
            else {
                 return true;
            }
        } else {
             return false;
        }
    }

    public static void main(String args[]) {

        JavaApplication obj = new JavaApplication(2021);

       if (obj.isLeapYear()) {
           System.out.println("Leap Year");
       } else {
           System.out.println("Not Leap Year");
       }

    }

}

Parameterized constructor can have multiple arguments.

If we need to create another object with same initialized param as we have previous created object, We can use copy constructor instead.

Copy Constructor

 public JavaApplication(JavaApplication obj) { // Parameterized constructor with argument as object of type JavaApplication

        this.year = obj.year;

    }

JavaApplication obj = new JavaApplication(2021);
JavaApplication copyObj = new JavaApplication(obj);

Read What is a Copy Constructor In Java for more details.