Constructor and Its Types in Java

Advertisements

Java Constructor

Constructor is a special function used to initialize class data members or we can say constructor is used to initialize the object of class.

Characteristics of constructor

  • Constructor name class name must be same.
  • Constructor doesn't return value.
  • Constructor is invoked automatically, when the object of class is created.

Types of Constructor

  • Default Constructor
  • Parameterize Constructor
  • Copy Constructor

Default Constructor

Constructor without parameter is called default constructor.

Example of default constructor


    //Employee.java

    class Employee
    {
        int Id;
        String Name;
        int Age;
        long Salary;

        Employee()           // Default Constructor
        {
            Id = 1;
            Name = "Kumar";
            Age = 29;
            Salary = 45000;
        }

        void PutData()
        {
            System.out.print("\n\tEmployee Id     : "+Id);
            System.out.print("\n\tEmployee Name   : "+Name);
            System.out.print("\n\tEmployee Age    : "+Age);
            System.out.print("\n\tEmployee Salary : "+Salary);
        }

        public static void main(String args[])
        {

            Employee E = new Employee();// Creating Object

            E.PutData();          // Calling PutData()

        }
    }

    Output :

                Employee Id     : 1
                Employee Name   : Kumar
                Employee Age    : 29
                Employee Salary : 45000

Parameterize Constructor

Constructor with parameter is called parameterize constructor.

Example of parameterize constructor


    //Employee.java

    class Employee
    {
        int Id;
        String Name;
        int Age;
        long Salary;

        Employee(int i,String nm,int a,long s)           // Parameterize Constructor
        {
            Id = i;
            Name = nm;
            Age = a;
            Salary = s;
        }

        void PutData()
        {
            System.out.print("\n\tEmployee Id     : "+Id);
            System.out.print("\n\tEmployee Name   : "+Name);
            System.out.print("\n\tEmployee Age    : "+Age);
            System.out.print("\n\tEmployee Salary : "+Salary);
        }

        public static void main(String args[])
        {

            Employee E = new Employee(2,"Sumit",31,35000);
            // Creating object and passing values to constructor.

            E.PutData();          // Calling PutData()

        }
    }

    Output :

                Employee Id     : 2
                Employee Name   : Sumit
                Employee Age    : 31
                Employee Salary : 35000

In parameterize constructor, we have to pass values to the constructor through object.

Copy Constructor

Example of copy constructor



Initializing Class Data Member Directly

In C++, we couldn't initialize class data members directly but in Java we can initialize class data member directly as given below.


    //Employee.java

    class Employee
    {
        int Id = 1;
        String Name = "Kumar";
        int Age = 29;
        long Salary = 45000;

        void PutData()
        {
            System.out.print("\n\tEmployee Id     : "+Id);
            System.out.print("\n\tEmployee Name   : "+Name);
            System.out.print("\n\tEmployee Age    : "+Age);
            System.out.print("\n\tEmployee Salary : "+Salary);
        }

        public static void main(String args[])
        {

            Employee E = new Employee();// Creating Object

            E.PutData();          // Calling PutData()

        }
    }

    Output :

    Employee Id     : 1
    Employee Name   : Kumar
    Employee Age    : 29
    Employee Salary : 45000

Direct initialization of data members are possible when there is no parameterize consructor and if we put default constructor explicitly, default constructor will overwrite all the values.

Advertisement