The answer is heap memory.
Before clearly reading the Java class initialization process,
But suddenly see such a topic, still ignorant.
After Baidu, those post reply each have each say,
The head can see the fortress live, or read the book to prove it.
The 128th page of Li Gang's crazy Java begins with a class that starts with initialization,
The detailed process of what happens in memory is briefly recorded here.
Class person{String name; static int eyenum;}
Above this person class, there is a member variable name and a static member variable Eyenum,
When the following statement is executed:
person P1 = new person (); person P2 = new Person ();p 1.name = "A";p 2.name = "B" p1.eyenum = 2;p2.eyenum = 3;
person P1 = new person (); The first time you use the person class, the system allocates space for the person class in heap memory, which also includes the static member variable Eyenum (the initial value is 0).
Then, in heap memory new a person object, where name is null, and then point P1 to the object.
Then new P2, because the class has been initialized, just repeat the 2nd step.
P1.name = "a"; Let the name of the P1 object in the heap memory point to a "a" string.
P1.eyenem = 2; The Eyenum property of the person class is assigned a value of 2 by modifying the class property of person (because it is a static member variable) through the Person object.
|
|
|
Person class
|
|
|
|
|
|
|
|
|
|
Eyenum→2
|
|
|
|
|
|
|
P1
|
------
|
-- |
---------------- |
--- |
→ |
|
|
|
|
|
|
→ |
Second Person object
|
|
|
First Person Object
|
|
|
|
P2
|
------
|
|
name→
|
B |
|
name→
|
A |
|
|
|
|
|
|
|
|
|
|
|
|
Stack memory
|
|
|
Heap
|
|
Within
|
|
Save
|
|
|
It's hard to see, but that's probably the way it is.
is the member variable initialized in the Java class stored in heap memory or in stack memory?