Java infinite recursion constructor instance details

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.