Java at debug time, some variables can be expanded indefinitely (loop)?

Source: Internet
Author: User

When throwing exceptions, when Java Debug, some variables can be infinitely expanded, how to do it?

Let's start with an example of an error:

Exception in thread "main" Java.lang.StackOverflowError
At llj.mf.ace.c.<init> (c.java:3)
At llj.mf.ace.b.<init> (b.java:5)
At llj.mf.ace.c.<init> (c.java:5)
At llj.mf.ace.b.<init> (b.java:5)
At llj.mf.ace.c.<init> (c.java:5)
At llj.mf.ace.b.<init> (b.java:5)
At llj.mf.ace.c.<init> (c.java:5)
At llj.mf.ace.b.<init> (b.java:5)
 ....
 ....

Code for Error:

public class Ace {public static void main (string[] args) {new B ();}} public class B {c c = new C ();} public class C {b b = new B ();}

Cause of error: When you create a B object, the B object creates a C object, and the C object creates a B object ... This creates countless B objects, C objects, so it stackoverflowerror .

An example that can be infinitely expanded:

/** * This debug, there is an infinite number of subordinates (you have me, I have you) */public class BCBC {public static void main (string[] args) {b b = new B (); c C = new C (); B.C = c;c.b = b; System.out.println (B.equals (c)); Breakpoint at}static class B {c C;} Static Class C {b b;}}

The above example debugs:

This creates only a B object, a C object, and then references each other (pointing to the other's address). (I pointed at you, you pointed at me: B <------> C)

This explains why some variables can be expanded indefinitely when Java Debug is thrown.

Java at debug time, some variables can be expanded indefinitely (loop)?

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.