Creation and recycling of Java variables
Member variables (instance variables):
Member variables are variables defined in a class, and each instance of a class has its own member variable. Their life cycle corresponds to the creation and destruction of objects.
- Creation time: Member variables are initialized when the object is created, and each object has an independent copy.
- Recycling time: When the object is no longer referenced, the garbage collector will recycle the memory space occupied by the object and will also recycle the object's member variables.
For example, consider the following code:
public class MyClass { private int count; // Member variables public void increment() { count++; } }
In the above code,count
is a member variable that belongs toMyClass
An instance of the class. Create one at a timeMyClass
When an object is used, a piece of memory will be allocated to the object to store it.count
value. When an object is no longer referenced, the object and its member variables are collected by the garbage collector.
Local variables:
Local variables are variables defined inside methods, code blocks, or constructors. Their life cycle is limited by the scope they are in.
- Creation time: Local variables are created when they are declared within the scope to which they belong.
- Recycling time: Once the scope of the variable is left, the variable will be destroyed and the occupied memory space will be released.
For example, consider the following code:
public void myMethod() { int num = 10; // Local variables // Other codes...}
In the above code,num
is a local variable, it only exists inmyMethod
The method is valid within the scope. When the method is executed,num
It will be destroyed and the memory space it takes will be freed.
Static variables (class variables):
Static variables are variables associated with classes rather than objects. They are initialized when the class is loaded, and all objects share a copy of the same static variable.
- Creation time: Static variables are initialized when the class is loaded and will only be initialized once.
- Recycling time: Static variables will be destroyed only when the program is finished running or the class is unloaded.
For example, consider the following code:
public class MyClass { public static int count; // Static variables // Other codes...}
In the above code,count
is a static variable, which belongs toMyClass
Class but not
is an instance of the class. AllMyClass
All objects share the samecount
copy of . When the program ends running orMyClass
When the class is uninstalled,count
It will be destroyed.
Summarize:
- The life cycle of a member variable corresponds to the creation and destruction of an object. Each object has an independent copy of the member variable.
- The life cycle of a local variable is limited by its scope and will be destroyed once it leaves the scope.
- The life cycle of a static variable corresponds to the loading and unloading of the class, and all objects share the same copy of the static variable.
Please note that this is just a brief introduction to the creation and recycling timing of member variables, local variables and static variables, and actually involves more complex garbage collection mechanisms and memory management. The specific situation will be affected by the implementation of the JVM and the operating environment. When writing code, you should select the appropriate variable type according to your needs and reasonably manage the life cycle and memory usage of the object.
This is the article about in-depth analysis of the creation and recycling timing of Java member variables, local variables and static variables. For more related content on creating and recycling Java variables, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!