简体   繁体   中英

Stack overflow error in recursive definition

I have the class definition n JAVA.

class ABC 
{ 
    public static final ABC A = new ABC();
    public static final ABC B = new ABC();
}

When I call, new ABC(), it runs fine without any error. But if I remove the word static, it runs into stack overflow error. I am aware that static fields are initiated once. But in this case, the initialization itself is recursive. So ideally even with static property, it should give the same error. Can you someone please explain. Thanks!

You answered the question yourself: static fields are initiated once. That's exactly why the code will not go into an infinite recursion. You can check this fact yourself attaching a debugger:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM