Constructors in Java with Example

In this chapter, we are going to learn about Constructors in Java. Before starting, I recommend reading Class and Package and Super, This, and Final keyword in Java.

1. What is Constructor in Java?

Constructors are a piece of code or usually a special method whose name is the same as the class name. Constructors are used for initializing objects and they are called just after the object creation.

Although, its not compulsory every time to define a constructor. If you don’t define a constructor in your Java class than a default constructor with no parameter will be created itself.

public class Vehicle{
   //This is the constructor
   Vehicle(){
   }
   // Rest of the code
}

Points to Remember:

  • It has the same name as the class.
  • Constructors don’t have any return type.
  • To initialize objects we use constructors.
  • Called Implicitly.
Constructors in Java

2. Types of Constructors in Java

There are two types of constructors in java i.e No Args Constructor, Parameterized Constructor.

2.1 No Args Constructor

Constructor which have no arguments.

Example:

public class TestClass {

	public TestClass() { // no args constructor

		System.out.println("This is default constructor");
	}
	public static void main(String[] args) {

		new TestClass(); // calling constructor
	}
}


2.2 Parameterized Constructor

Constructor which have N number of arguments.

Example:

public class TestClass {
	int a;
	int b;

	public TestClass(int firstParameter, int secondParameter) { // parameterized constructor with two parameters

		this.a = firstParameter; // initializing object a
		this.b = secondParameter; // initializing object b
	}
	public static void main(String[] args) {
		
		TestClass obj =  new TestClass(5, 7); // calling and passing value to constructor
		System.out.println("Value of a : " + obj.a);
		System.out.println("Value of b : " + obj.b);
	}
}

3. Constructor Overloading in Java

Have you heard about method overloading? Similarly, we can also overload constructors. Constructor overloading is a concept of having multiple constructors differentiated on the basis of the number of parameters.

public class TestClass {

	public TestClass() { 
		System.out.println("Constructor with No Parameter");
	}

	public TestClass(int firstParameter) { 
		System.out.println("Constructor with Single Parameter with value : " + firstParameter);
	}

	public TestClass(int firstParameter, int secondParameter) { 
		System.out.println("Constructor with Two Parameter with value : " + firstParameter + " and " + secondParameter);
	}

	public static void main(String[] args) {
		new TestClass();
		new TestClass(5);
		new TestClass(5, 7);
	}
}

Remember:

Every class has a default constructor. For instance, default constructor for class TestClass is TestClass(). However, if you define your own constructor then it becomes no-args constructor. Also, passing arguments to constructor known as a parameterized constructor.

In case you have parameterized constructor and you also need default constructor then it is mandatory to define explicitly.

4. Constructor Chaining in Java

In Java, calling a constructor inside another constructor is known as constructor chaining. There are two ways:

Must Read: Super, This, and Final keyword

  • Within the same class: By using this keyword.
public class TestClass {

	public TestClass() { // 1st constructor
		this(5); // calling 2nd constructor
		System.out.println("From Default Constructor");
	}

	public TestClass(int firstParameter) { // 2nd constructor
		this(5, 8); // calling 3rd constructor
		System.out.println("Value of firstParameter : " + firstParameter);
	}

	public TestClass(int firstParameter, int secondParameter) {
		// 3rd constructor
		System.out.println(firstParameter + secondParameter);
	}

	public static void main(String[] args) {
		new TestClass();
	}
}
  • From base class: By using super keyword.
public class BaseClass {

	public BaseClass() {
		System.out.println("From Base Class");
	}
}
public class ChildClass extends BaseClass {
	ChildClass() {
		super();
		System.out.println("From Child Class");
	}
	public static void main(String[] args) {
		new ChildClass();
	}
}

I hope you have learned Constructors in Java. Do share this post and spread the knowledge.

Happy Learning!