I just saw this weird piece of code in another question. I thought it would result in a StackOverflowError being thrown, but it does not.
public class Node {
public static Node NIL = new Node(Node.NIL, Node.NIL);
public Node(Object one, Object two) {
// Assign values to fields
}
}
I thought it was going to explode, because Node.NIL is referencing itself to build.
Can someone explain to me why it does not?
staticbut i am not sure – XtremeBaumer 22 hours agoNILfield is constructed as it it was declared asnew Node(null, null), because when the constructor is called,Node.NILhasn't been set to anything yet. – khelwood 22 hours agoStackOverflowErrorbecause you posted it on Stack Overflow, and that would look bad. Code these days is often context-aware, so if you posted it on (say) Facebook, it might throw aStackOverflowErrorunder those conditions, but would never throwFacebookError. Similarly, code posted on Reddit never triggers aRedditException. Honest! Stop looking at me like that. – Ti Strga 12 hours ago