Java constructor


A Java constructor is a special method which is used to initialize the object of the class.

It is called at the time of object creation and memory is allocated for the object.

A constructor constructs the values i.e. it provides data for the object, therefore it is known as constructor.

Every java class has atleast 1 constructor because if we do not explicitly create a constructor for a class then java compiler creates a default constructor for that class.

A class can have one or more than one constructor.

A constructor doesn’t have any return type but still it returns the current class instance.

Note: The Constructor name must be same as its class name and it should not have a return type.



Types of Constructor


There are 2 types of constructors in java:

1. Default constructor

2. Parameterized constructor


Default Constructor


A default constructor doesn't has any parameter.

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


Syntax:

className(){}

Example:

class Website
{
    // Default Constructor
    Website()
    {
        System.out.println("Welcome to TutorialsDuniya.com");
    }

    public static void main(String args[])
    {
        Website web = new Website();  
    }
}

Output:

Welcome to TutorialsDuniya.com

Parameterized Constructor


A parameterized constructor has parameters.

It is used to initialise different values to the different objects.


Syntax:

className(String parameters){}

Example:

class Company
{  
   	int empId;
   	String name;

    // parameterized constructor
   	Company(int id , String n)
    {
   		empId = id;
        name = n;
    }

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

    public static void main(String args[])
    {
    	Company c1 = new Company("Raj", 21);  
        Company c2 = new Company("Abhishek", 108);  
    	c1.display();
   		c2.display();
    }
}

Output:

Raj 21
Abhishek 108

Constructor Overloading


Java Constructor overloading is a technique of having more than one constructor with different parameter lists. They are differentiated by compiler by the number of parameters in the list and their data types.

Example: In this example, we are overloading the constructors by giving 2 parameters in first constructor and 3 parameters in second constructor. In first constructor, we are initialising two fields rollNo and name, we are not initialising age, hence age will be initialised by the default value of int i.e.0. In second constructor, we are initialising three fields rollNo, name and age.

class Student
{
	int rollNo;  
	String name;  
	int age;  
	Student(int rn, String n)
    {
        //age will get the default value of int i.e.0
	    rollNo = rn;
		name = n;  
    }

    Student(int rn, String n, int a)
    {
		rollNo = rn;  
	    name = n;  
		age = a;
    }

	void display()
	{
        System.out.println(rollNo+" "+name+" "+age);
    }

	public static void main(String args[])
    {
		Student s1 = new Student(7,"Abhishek");  
		Student s2 = new Student(21,"Raj",18);  
		s1.display();
		s2.display();
	} 
}

Output:

7 Abhishek 0
21 Raj 18