📜  Java的递归构造函数调用

📅  最后修改于: 2022-05-13 01:55:48.953000             🧑  作者: Mango

Java的递归构造函数调用

函数直接或间接调用自身的过程称为递归,对应的函数称为递归函数。在递归程序中,提供了基本情况的解决方案,并以较小问题的形式表示较大问题的解决方案。这里是Java的递归构造函数调用和堆栈溢出错误。如下例所示:

例子

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(); }
}


Java
// Java program to Illustrate How Recursive
// Constructor Invocation Error is Occured
  
// 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();
    }
}


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();
    }
}


输出
Call 1
Call 2
Call 3
Call 4
Call 5

递归构造函数调用

如果构造函数调用自身,则会出现错误消息“递归构造函数调用”。编译器不允许以下程序,因为在构造函数中我们试图调用相同的构造函数。编译器立即检测到它并抛出错误。



例子:

Java

// Java program to Illustrate How Recursive
// Constructor Invocation Error is Occured
  
// 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();
    }
}

输出:

现在让我们讨论堆栈溢出错误究竟指的是什么以及它为什么会发生如果我们没有为我们的递归函数或模板提供适当的终止条件,就会发生堆栈溢出错误,这意味着它将变成一个无限循环。

执行:

在这里,我们在构造函数中创建了一个 GFG 对象,该对象通过调用构造函数进行初始化,然后创建另一个 GFG 对象,该对象再次通过调用构造函数进行初始化,直到堆栈溢出。这可以从插图中得到证明,如下所示:

例子:

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();
    }
}

输出: