簡體   English   中英

為什么 class “測試”中的以下代碼不會使用 static 關鍵字給出 stackoverflow 錯誤

[英]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() ;  
}

得到了答案

  1. class 中的 static 變量在可以創建 ZA2F2ED4F8EBC2CBB4C21A29DC40AB6 的任何 object 之前初始化。

2)靜態變量,方法與 class 相關,而不是特定實例。

因為 p 是 static - 它只初始化一次,所以 new test() 不會第二次執行 - 因此沒有堆棧溢出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM