Look at the following class:
class Cat ( int ID; String name; static int SID; Cat (String name) { this . Name = name ;
ID = sid++; public static void main (string[] args) {cat.sid = 100; Cat Mimi = new Cat ("Mimi" = new Cat ("Pipi"
The allocation of memory is as follows:
1, the first line of the Main method cat.sid = 100
Because the SID is a static variable, regardless of whether the cat class created the object or not, a few objects were created, only a chunk of memory is allocated in data segment, the type is int, and the name is Sid, and the value is 100.
2, the second row new a Cat object, first allocates a piece of memory in the stack, puts the local variable Mimi, and then constructs a cat object in the heap, which has the ID and the name attribute.
String is actually a class, "Mimi" is a string constant that allocates a chunk of memory in data segment, so the Name property is a reference to "Mimi".
3, the third line to build a Cat object Pipi, in the same vein, but at this time the SID becomes 102.
The last memory allocation is as follows:
Java Handy Sticker 3