将对象作为类变量会导致stackoverflow错误
public class stack {
stack obj = new stack(); // its obvious that during class loading obj will call class to
// load and infinite loop will occur.
}
假设我在类对象中使用
static
。public class stack {
static stack obj = new stack(); // it will not cause infinite loop and program will //execute successfully
}
当JVM第一次捕获类时,静态变量将分配到内存中(据我所知)。
例如,在第一次分配内存给上述static
对象变量时。它会再次调用类,这应该也会导致无限循环。但是我可能有错误,能否有人指出我的错误所在。