2
下面是我的一位朋友在討論中提出的一個問題,但我們都不知道實際答案是什麼。在java中創建對象的位置
public class Test {
static int i = 5;
static String str = "Welcome";
static Map<String, String> map = new HashMap<>();
}
在目標代碼,創建JVM實例,它會創建三個對象,它在哪裏創建它
- Inside memory/metadata area
- Inside heap area
我們知道,memory area
擁有與類中的所有數據時(包括靜態引用)和heap area
保存一個類的所有實例變量。
但對於上述
- int variable is created(We assume that it will be created in method area
- Not sure about the other two object(String and HashMap)
類糾正我們,如果我們誤解了什麼,並幫助我們瞭解這些變量將如何在Java
如您所述,所有對象都在堆上創建。 –
* 1錯誤*:靜態字段在加載類時被初始化。 –