Difference analysis:
(Recommended tutorial: java introductory tutorial)
1. Syntax difference
Static variables need to be modified with the static keyword, but instance variables do not.
2. The difference when the program is running
Static variables are subordinate to classes, and instance variables are subordinate to objects.
Instance variables must create an instance object, and the instance variables in it will be allocated space before this instance variable can be used; static variables are class variables. As long as the program loads the bytecode of the class, the static variables will Once the space is allocated, it can be used.
Summary: Instance variables must be used through this object after creating an object. Static variables can be referenced directly using the class name.
(Video tutorial recommendation: java video tutorial)
Note: The use of (static) static variables also has limitations. A static method cannot call a class Non-static methods and variables, static-modified variables have only one memory space in the memory after the class is loaded, and can be shared by all instance objects of a class.
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!