Differences between member variables and local variables:
Differences between member variables and local variables:
1. Different Positions in the class
Member variable: in the class, out of the Method
Local variables: In method definition or method declaration
2. The locations in the memory are different:
Member variable: In heap memory
Local variable: in stack memory
3. different lifecycles:
Member variable: It exists with the creation of the object and disappears with the disappearance of the object
Local variable: It exists with the call of the method and disappears with the completion of the call of the method.
4. Different initialization values
Member variable: with default initialization value
Local variable: No Default initialization value. It must be defined and assigned values before use.
Note:
The local variable name can be the same as the member variable name. the proximity principle is used in methods.