Constructor in Java
0% completed
Constructor in Java
Constructor in Square Class
Properties of a Constructor
Return Type
Constructor Calling
Constructor Calling II
Is Possible?
Basics Of Java
Constructor in Java

 

Constructor in Java

footer line

What is a Constructor?

 

A constructor is a member function of a class, and its name is the same as the name of the class. When there is no constructor defined inside the class, the Java compiler provides a default constructor in the class. In Java, every class has a default constructor. The constructor has no return type. A constructor can be parameterized or overloaded.

 

 

When is a Constructor called?

 

When we use the new keyword to create an object of a class, a constructor is invoked to initialize the data members of the class. 

 

Example:

 

public class ConstructorExample {
    ConstructorExample () {
        /*
        Body of Constructor
         */
    }

    public static void main(String[] args) {
        // Invoking Constructor
        ConstructorExample obj = new ConstructorExample();
    }
    
}

 

 

Rules for Creating a Constructor

  • The constructor and class names must be the same.
  • There is no return type in a constructor.
  • Constructors in Java can never be static, final, or abstract.

 

 

Types of Constructor in Java

 

There are two types of constructors in Java.

  • No-argument or default constructor
  • Parameterized constructor

 

i) No-argument constructor

 

When a programmer does not define a constructor in the program, the java compiler creates a default constructor for the class. In Java, this constructor is referred to as the default constructor. 

 

Example:  

 

public class ConstructorExample {

    // Default Constructor
    ConstructorExample () {
        System.out.println("Coding Ninjas");
    }

    public static void main(String[] args) {
        // This would invoke Default Constructor of the class
        ConstructorExample obj = new ConstructorExample();
    }
    
}

 

Output: 

 

Coding Ninjas


 

ii) Parameterized constructor

 

A parameterized constructor is one that accepts one or more parameters. Whenever we create an object of the class with a parameterized constructor, we need to pass the argument so that the constructor gets invoked after the object creation.

 

Example:

 

public class ConstructorExample {

    // Data Memnber
    String str;

    // Parameterize Constructor
    ConstructorExample (String s) {
        str = s;
    }

    public void show() {
        System.out.println(str);
    }

    public static void main(String[] args) {
        // This would invoke Parameterize Constructor of the class
        ConstructorExample obj = new ConstructorExample("Coding Ninjas");
        obj.show();
    }
    
}

 

Output:

 

Coding Ninjas

 

 

Constructor Overloading in Java

 

In Java, constructor overloading occurs when a class has several constructors with different parameters. In constructor overloading, every constructor performs different tasks.

 

Example:

 

class Employee {

    // Data Members
    String name;
	int employeeId;
	int salary;

	// Constructor with one argument
	Employee(int eId) {
		employeeId = eId;
	}

	// Constructor with two arguments
	Employee(int eId, String s) {
		employeeId = eId;
		name = s;
	}

	// Constructor with three arguments
	Employee(int eId, String s, int sal) {
		employeeId = eId;
		name = s;
		salary= sal;
	}

	// Method to display employeeId, name, and salary
	public void show() {
		System.out.println(employeeId + " " + name + " " + salary);
	}
}

public class ConstructorOverloadingExample {
	public static void main(String args[]) {

		// Passing the values in the constructor
		Employee obj1 = new Employee(265);
		Employee obj2 = new Employee(895, "Shiv");
		Employee obj3 = new Employee(236, "Afzal", 70000);

		// Displaying the result
		obj1.show();
		obj2.show();
		obj3.show();
	}

}

 

Output: 

 

265 null 0
895 Shiv 0
236 Afzal 70000