Member variables:
When the system loads the class or creates an instance of the class, the system automatically allocates the memory space for the member variable and automatically assigns the initial value to the member variable after allocating the memory space.
Eyenum is a class property. Name is an instance property
All person instance access Eyenum will access the Eyenum property of the person class. Access is the same piece of memory. If you modify a class property through an instance, all instances will be accessed with a change.
Local variables:
After a local variable is defined, it must be displayed for initialization before it can be used. This means that when a local variable is defined, the system does not allocate memory space for the variable until the program assigns the initial value to the variable, and the local variable is allocated memory and the initial value is saved to this memory.
Unlike member variables, a local variable does not belong to any class or instance, so it is always stored in the stack memory of the method in which it resides. If the local variable is a basic type variable, the value of the variable is stored directly in the stack, and if it is a reference type, the address is stored in the stack.
The variables in the stack memory do not need to be garbage collected by the system, and the variables end with the execution of the method or block of code. Therefore, the scope of a local variable starts with the initialization of the variable, knowing that the method or the block of code is finished running. Because local variables only hold values of the base type or references to objects, local variables typically take up less memory.
When we define a member variable, the member variable is placed into the heap memory, and the scope of the member variable expands to the scope of the class existence or the object exists. There are two disadvantages to this range enlargement:
1, increase the lifetime of the variable, will lead to greater system overhead
2, expanding the scope, not conducive to the cohesion of the program.
Initialization of Java member variables and local variables and operating mechanisms in memory