Recursive Constructor Invocation in Java


Recursive Constructor Invocation is a compile time error that occurs when a constructor calls itself. It is similar to recursion where a method calls itself as often as necessary. The method that calls itself is called as recursive method and the constructor that calls itself is called as recursive constructor.

In this article, we will understand the Recursive Constructor Invocation error in Java with a few examples.

Recursive Constructor Invocation

Constructor

It is quite similar to methods but the difference is that methods define the behavior of an object but constructor is used to initialize those objects. We can provide any name of our choice to methods but a constructor must have the same name as class name. Also, methods can return a value but constructor does not return any value because they can’t have any return type.

When a user does not create any constructor, then the Java compiler will automatically create one (we call it as default constructor).

Example 1

public class Cnst { // class name
   Cnst() { 
     // constructor 
     System.out.println("I am constructor");
   }
   public static void main(String[] args) {
     Cnst obj = new Cnst();  
     // calling the Constructor
   }
}

Output

I am constructor

Despite the similarities between a constructor and a method, Java does not allow a recursive constructor. It is a bad programming practice.

Example 2

The following example illustrates the recursive constructor invocation error.

Here, we will create a class and define its constructor along with two parameters. Then, we will call the same constructor within its body.

public class Cart {
   String item;
   double price;
   Cart(String item, int price) { // constructor
     this(item, price); // constructor is calling itself
	// this keyword shows these variables belong to constructor
     this.item = item; 
     this.price = price;
   }
   public static void main(String[] args) {
     Cart obj = new Cart("Bread", 15); // creating object
     System.out.println("Constructor calling another Constructor");
   }
}

Output

Cart.java:4: error: recursive constructor invocation
Cart(String item, int price) { // constructor
   ^
1 error

Example 3

In the following example, we will try to define an object inside the constructor to check if Java allows the object creation inside constructor.

public class Cart {
   String item;
   double price;
   Cart(String item, int price) { 
   // constructor
	// this keyword shows these variables belong to constructor
     this.item = item; 
     this.price = price;
     Cart obj2 = new Cart("Milk", 55); 
     // creating object
   }
   public static void main(String[] args) {
     Cart obj1 = new Cart("Bread", 15); 
     // creating another object
     System.out.println("Constructor calling another Constructor");
   }
}

Output

Exception in thread "main" java.lang.StackOverflowError
	at Cart.<init>(Cart.java:9)
	at Cart.<init>(Cart.java:9)
	at Cart.<init>(Cart.java:9)
	at Cart.<init>(Cart.java:9)
	at Cart.<init>(Cart.java:9)
	at Cart.<init>(Cart.java:9)
	at Cart.<init>(Cart.java:9)

We are getting StackOverflowError because creating an object inside the constructor leads to an infinite loop of object creation.

Example 4

The following example demonstrates that it is legal to call a constructor inside another constructor.

public class Cart { 
   // class
   String item;
   double price;
   Cart(String item, int price) { 
   // first constructor
	// this keyword shows these variables belong to constructor
     this.item = item; 
     this.price = price;
   }
   public Cart (int price) { 
   // second constructor
     this(null, price); 
     // calling the 1st constructor
   }
   public static void main(String[] args) {
     Cart obj = new Cart(15); 
     // creating object
     System.out.println("Constructor calling another Constructor");
   }
}

Output

Constructor calling another Constructor

Conclusion

Java does not allow the recursion of constructors therefore, it is obvious to avoid this programming practice. In this article, we have started by discussing constructor and tried to explain recursive constructor. Also, we discovered another error named StackOverflowError that occurs due to an infinite loop.

Updated on: 16-May-2023

783 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements