當我們實體化一個類時,它存盤在哪里?以及物件內部的值存盤在哪里。
例子:
MyDemoClass obj1 = new MyDemoClass();
obj1 存盤在哪里?
如果我這樣做
obj1.x = 10;
x 存盤在哪里?
這 10 個存盤在哪里?
如果我們將變數 x 設為靜態
class MyDemoClass {
static int x;
MyDemoClass() {
x = 10;
}
}
現在 x 存盤在哪里,值 10 存盤在哪里?
有人可以用簡單的話解釋嗎?
感謝您抽出寶貴的時間來解決這個新手疑問!
uj5u.com熱心網友回復:
MyDemoClass obj1 = new MyDemoClass();
obj1 存盤在哪里?
obj1 is just a label/pointer. In memory it will be assigned to the stack. The object that obj1 points to will be on the heap (all objects in Java exist on the heap)
如果我這樣做
obj1.x = 10;
x 存盤在哪里?
x is stored on the heap with the object
如果我們將變數 x 設為靜態,這 10 存盤在哪里?
x is then stored on the heap, associated with the MyDemoClass Class object
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/507301.html
