Explanation of class variables, member variables, instance variables, local variables, static variables, and global variables

1. The relationship between:
global variables are commonly known as member variables; class variables are variables modified by the keyword static, also known as static variables; the member variables (global variables) of java classes include two types: one is the static key Word-modified variables are called class variables or static variables, and the other ones that are not modified by static are called instance variables.
2. Local variables and member variables:
the former has no default initialization value, and must be declared and assigned to be used; the latter has a default initialization value, which can be used directly after declaration.
3. Location and life cycle in memory:
    1) There is only one static variable of a class in memory. When the class is loaded, memory is allocated for static variables. Static is located in the method area and is shared by all instances of the class. Static variables It can be accessed directly by the class name, and its lifetime depends on the lifetime of the class. Sometimes, in order to save memory and share resources, variables in a class can be declared as static variables. However, the declaration period of static variables is too long, and it is not easy to be recycled by the system. Improper use will result in a waste of memory.
    2) Instance variables depend on the instance of the class. Every time an instance is created, the Java virtual machine allocates memory for instance variables. Instance variables and objects are located in the heap area, and their life cycle depends on the declaration cycle of the instance.
    3) The local variable is declared in a method or code block (such as a for loop), when it is executed, it directly opens up space in the stack and uses it, and directly releases the memory after execution, which is very fast.
Summary: Local variables are located in the stack area, static variables are located in the method area, instance variables are located in the heap area, objects are located in the heap area, and object references are located in the stack area.
4. Differences between static variables and instance variables:
   1) Differences in syntax definition: Static variables are modified by the keyword static. Instance variables are not modified by static.
   2) The difference when the program is running: instance variables belong to the attributes of an object, and the object must be created before the instance variables are allocated space, while static variables belong to the class, and the space will be allocated when the class is loaded.
   3) Ways of use: The instance variable is called by the object after the class is instantiated. Static variables are called directly by the class name.

Forward to: https://www.douban.com/note/513499371/

CSDN:https://blog.csdn.net/fanxiaobin577328725/article/details/54755378

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324769143&siteId=291194637