Recursive Constructor Invocation in Java Last Updated : 26 Sep, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. In the recursive program, the solution to the base case is provided and the solution of the bigger problem is expressed in terms of smaller problems. Here recursive constructor invocation and stack overflow error in java. It is as shown below in the example as follows: Example Java // Java Program to Illustrate Recursion // Main class public class GFG { static int count = 0; // Method 1 // Recursive method static void function() { count = count + 1; if (count <= 5) { System.out.println("Call " + count); function(); } } // Method 2 // Main driver method public static void main(String[] args) { function(); } } OutputCall 1 Call 2 Call 3 Call 4 Call 5 Recursive Constructor Invocation If a constructor calls itself, then the error message "recursive constructor invocation" occurs. The following program is not allowed by the compiler because inside the constructor we tried to call the same constructor. The compiler detects it instantly and throws an error. Example: Java // Java program to Illustrate How Recursive // Constructor Invocation Error is Occurred // Main class class GFG { // Constructor of this class // Inside we are trying to call the same constructor GFG() { // This keyword refers to same instance itself this(); } // Main driver method public static void main(String[] args) { // Creating an object of class inside main() GFG obj = new GFG(); } } Output: Now let us discuss what exactly we do refer to by Stack overflow error and why does it occur. Stack overflow error occurs if we do not provide the proper terminating condition to our recursive function or template, which means it will turn into an infinite loop. Implementation: Here we have created a GFG object inside the constructor which is initialized by calling the constructor, which then creates another GFG object which is again initialized by calling the constructor and it goes on until the stack overflows. This can be justified from the illustration as follows: Example: Java // Java program to Illustrate Stack Overflow Error // Main class public class GFG { // Constructor of this class GFG() { // Creating an object of GFG class inside the // constructor which is initialized by calling the // constructor, which is initialized by // calling the constructor and it goes on GFG obj1 = new GFG(); } // Main driver method public static void main(String[] args) { // Creating an object of this class GFG obj = new GFG(); } } Output: Comment More infoAdvertise with us Next Article Recursive Constructor Invocation in Java D dattabikash505 Follow Improve Article Tags : Java Blogathon Blogathon-2021 Java-Constructors Practice Tags : Java Similar Reads Properties of Constructors in Java A constructors in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes. In Java, a constructor is a block of codes similar to the method. It is called when an instance o 3 min read Constructor Overloading in Java Java supports Constructor Overloading in addition to overloading methods. In Java, overloaded constructor is called based on the parameters specified when a new is executed. When do we need Constructor Overloading? Sometimes there is a need of initializing an object in different ways. This can be do 5 min read java.lang.reflect.Constructor Class in Java java.lang.reflect.Constructor class is used to manage the constructor metadata like the name of the constructors, parameter types of constructors, and access modifiers of the constructors. We can inspect the constructors of classes and instantiate objects at runtime. The Constructor[] array will hav 4 min read Inheritance and Constructors in Java Constructors in Java are used to initialize the values of the attributes of the object serving the goal to bring Java closer to the real world. We already have a default constructor that is called automatically if no constructor is found in the code. But if we make any constructor say parameterized 3 min read Default constructor in Java Like C++, Java automatically creates default constructor if there is no default or parameterized constructor written by user, and (like C++) the default constructor automatically calls parent default constructor. But unlike C++, default constructor in Java initializes member data variable to default 1 min read Like