[英]why this code don't give me the same result ? situation with static and static {}
[英]Why the following code in class “test” don't give stackoverflow error with static keyword
为什么 class test
中的以下代码不会使用static
关键字给出 stackoverflow 错误?
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
test t = new test();
}
}
class test{
static test p = new test() ;
}
得到了答案
2)静态变量,方法与 class 相关,而不是特定实例。
因为 p 是 static - 它只初始化一次,所以 new test() 不会第二次执行 - 因此没有堆栈溢出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.