The difference between member variables and local variables
(1) The position in the class is different
成员变量:类中方法外
局部变量:方法定义中或者方法声明上
(2) Different locations in memory
成员变量:在堆内存中
局部变量:在栈内存中
(3) Different life cycles
成员变量:随着对象的创建而存在,随着对象的消失而消失
局部变量:随着方法的调用而存在,随着方法的调用完毕而消失
(4) Different initialization values
成员变量:有默认值
局部变量:没有默认值,必须定义,赋值,然后才能使用
Note: The name of a local variable can be the same as the name of a member variable. When used in a method, the principle of proximity is adopted
The difference between static variables and member variables
A: Different belonging
静态变量:属于类,类变量
成员变量:属于对象,对象变量,实例变量
B: different memory locations
静态变量:方法区的静态区
成员变量:堆内存
C: Different life cycles
静态变量:静态变量是随着类的加载而加载,随着类的消失而消失
成员变量:成员变量是随着对象的创建而存在,随着对象的消失而消失
D: call is different
静态变量:可以通过对象名调用,也可以通过类名调用
成员变量:只能通过对象名调用
Conducive to understanding the elements stored in the virtual machine heap, stack, and method area.
Reprint address:
https://blog.csdn.net/u010273362/article/details/50451006