This article will introduce you to an example of java infinite recursion constructor, if you are interested in this, please refer to it. I hope this tutorial will give you some understanding of adding an infinitely recursive constructor.
In some cases, the program can cause the constructor to carry out infinite recursion, such: Public class ConstrucorRecursion { { ConstrucorRecursion rc = new ConstrucorRecursion (); } Public ConstrucorRecursion () { System. out. println ("constructor without parameters for Program Execution "); } Public static void main (String [] args) { ConstrucorRecursion rc = new ConstrucorRecursion (); } } |
|
Public class InstanceTest {private String name; // private InstanceTest instance holding the current class; // defines a non-parameter constructor public InstanceTest () {} // defines the parameter-specific constructor public InstanceTest (String name) {// call the parameter-free constructor to initialize the intance instance = new InstanceTest (); instance. name = name;} // rewrite toString () method public String toString () {return "InstanceTest [instance =" + instance + "]";} public static void main (String [] args) {InstanceTest in = new InstanceTest (); InstanceTest in2 = new InstanceTest ("test name"); // Let two objects reference each other in. instance = in2; in2.instance = in; System. out. println (in); System. out. println (in2 );}}