i create a class as below:
public class NodeOne {
NodeOne nodeone = new NodeOne();
public NodeOne() {
System.out.println("1");
}
public static void main(String[] args) {
NodeOne nodeone = new NodeOne();
}
}
once i run it i got exception "Exception in thread "main" java.lang.StackOverflowError ",which i know the reason for sure. but when i add the modifier "static" before the field nodeone, the result turn out to be fine. so what's the deep cause?
public class NodeOne {
static NodeOne nodeone = new NodeOne();
public NodeOne() {
System.out.println("1");
}
public static void main(String[] args) {
NodeOne nodeone = new NodeOne();
}
}
output:
1
1