Static variables belong to the class level, while instance variables belong to the object level.
There are two main differences between static variables and instance variables:
1. Different storage locations
Class variables exist in In the method area, instance variables exist in the heap memory as the object of the object is created.
2. Different life cycles
Class variables have the longest life cycle. They are loaded as the class is loaded and disappear as the class disappears. Instance variables disappear as the object disappears. .
Notes on static usage:
1. Static methods can only access static members (including member variables and member methods), but cannot access non-static members or methods.
Non-static methods can access static or non-static methods or members.
2. This and super keywords cannot appear in static methods.
Because static precedes the existence of objects, this and super keywords cannot appear.
3. The main function is static.
The above is the detailed content of What is the difference between static variables and instance variables in Java?. For more information, please follow other related articles on the PHP Chinese website!