![](/img/trans.png)
[英]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.