Home >Java >javaTutorial >A brief discussion on the differences between static variables, member variables and local variables
The difference between static variables and member variables:
A: Different ownership
Static variables: belong to classes, class variables
Member variables: belong to objects, object variables, Instance variables
B: Different memory locations
Static variables: Static area of method area
Member variables: Heap memory
C: Different life cycles
Static variables: Static variables are loaded as the class is loaded, and disappear as the class disappears
Member variables: Member variables exist with the creation of the object, and disappear as the object Disappear and disappear
D: Call different
Static variables: can be called through the object name, or can be called through the class name
Member variables: can only be called through the object name
The difference between member variables and local variables:
A: Different locations in the class
Member variables: outside the methods in the class
Local variables: In the method definition or method declaration
B: Different locations in memory
Member variables: in heap memory
Local variables: in stack memory
C: Different life cycles
Member variables: exist with the creation of the object, and disappear with the disappearance of the object
Local variables: exist with the call of the method, and disappear with the invocation of the method The method disappears after the call is completed
D: Different initialization values
Member variables: have default initialization values
Local variables: no default initialization values, must be defined, assigned, and then can be used.
Note:
The local variable name can be the same as the member variable name. When used in a method, the proximity principle is adopted.
The above is all the content that the editor brought to you to briefly talk about the differences between static variables, member variables, and local variables. I hope it will be helpful to everyone. Please support the PHP Chinese website~
For more articles on the differences between static variables, member variables, and local variables, please pay attention to the PHP Chinese website!