First, instance variables
Also called an object variable, a class member variable, the storage space is allocated when the object is generated from the class, and the instance variables between the objects do not interfere with each other, and the instance variables can be accessed through the object's reference. However, in Java Multi-threading, instance variables are multiple threads that share resources, and you should be aware of the problems that may occur when you synchronize access.
[Java]View PlainCopy
- <span style="FONT-SIZE:14PX;" >public class Demo {
- //The following are instance variables (member variables, object variables)
- private String namestring;
- public int age;
- protected int priority;
- //Instance method
- Public String getnamestring () {
- return this.namestring;
- }
- }</span>
Second, class variables
Also known as a static variable, is a special instance variable, decorated with the static keyword, a class of statically variable, all the objects generated by this class share this class variable, the class load allocates storage space. When an object modifies a variable, the value of the variable in the object changes.
[Java]View PlainCopy
- <span style="FONT-SIZE:14PX;" >public class Demo {
- //class variable (static variable)
- public static int a = 0;
- //Instance variable
- private String namestring;
- }</span>
Third, local variables
A parameter in a method or a local block that declares a defined variable or method is called a local variable, and they exist only in the block where they were created ({}) and cannot perform any operations outside the block, such as reading or assigning values. In Java Multi-threading, each thread replicates a local variable to prevent some synchronization problems from occurring.
[Java]View PlainCopy
- <span style="FONT-SIZE:14PX;" > //class variables (static variables)
- public static int a = 0;
- //Instance variable
- private String namestring;
- public Void Test () {
- //Local variables
- int temp = 1;
- SYSTEM.OUT.PRINTLN (temp);
- }</span>
Iv. the difference between them
The difference between a member variable and a local variable
Member variables:
The ① member variable is defined in the class and can be accessed throughout the class.
The ② member variable is created as the object is built, disappears as the object disappears, and exists in the heap memory where the object resides.
The ③ member variable has a default initialization value.
Local variables:
① Local variables are defined only within the local scope, such as within a function, within a statement, and only in the area to which they belong.
② local variables exist in the stack memory, the scope of the action ends, and the variable space is automatically freed.
③ local variable has no default initialization value
The principles to be followed when using variables are: proximity principle
First, in the local scope to find, have to use; then find it in the member location
The difference between a member variable and a static variable
1, two variables have different life cycles
Member variables exist as objects are created and released as objects are recycled.
Static variables exist as the class loads, disappearing as the class disappears.
2. Different calling methods
Member variables can only be called by an object.
A static variable can be called by an object and can also be called by a class name.
3. Different aliases
Member variables are also known as instance variables.
A static variable is also known as a class variable.
4. Different Data storage locations
Member variables are stored in the object of the heap memory, so they are also called object-specific data.
Static variable data is stored in the static area of the method area (shared data area), so it is also called the shared data of the object.
Interview questions: Java instance variables, local variables, class variables