javaThere are two types of member variables of a class: one is a variable modified by the static keyword, called a class variable or static variable; the other is a variable without static modification, called Instance variables.
The difference in syntax definition: the static keyword must be added before static variables, but not before instance variables.
The difference when the program is running: Instance variables belong to the attributes of an object. An instance object must be created before the instance variable in it will be allocated space and this instance variable can be used. Static variables do not belong to an instance object, but to a class, so they are also called class variables. As long as the program loads the bytecode of the class without creating any instance objects, the static variables will be allocated space, and the static variables can be used. In short, instance variables must create an object before they can be used through this object, while static variables can be referenced directly using the class name.
For example, for the following program, no matter how many instance objects are created, only one staticVar variable is always allocated, and every time an instance object is created, this staticVar will be increased by 1; however, every time an instance object is created, , one instanceVar will be allocated, that is, multiple instanceVar may be allocated, and the value of each instanceVar is only incremented once.
public class VariantTest { public static int staticVar = 0; public int instanceVar = 0; public VariantTest() { staticVar++; instanceVar++; System.out.println(“staticVar = ”++staticVar “, instanceVar = ” ++instanceVar); } }
There is only one static variable of the class in the memory. The Java virtual machine allocates memory for the static variable during the process of loading the class. The static variable is located in the method area and is shared by all instances of the class. Static variables can be accessed directly through the class name, and their life cycle depends on the life cycle of the class.
And instance variables depend on the instance of the class. Every time an instance is created, the Java virtual machine allocates memory for instance variables. The instance variables are located in the heap area, and their life cycle depends on the life cycle of the instance.
public class Temp { int t; //实例变量 public static void main(String args[]){ int t=1; //局部变量 System.out.println(t); //打印局部变量 Temp a= new Temp(); //创建实例 System.out.println(a.t); //通过实例访问实例变量 } }
The result is:
1
0 (member variables have default values but local variables do not)
Change the code to:
public class Temp { static int t; //类变量 public static void main(String args[]){ System.out.println(t); //打印类变量 int t=1; //局部变量 System.out.println(t); //打印局部变量 Temp a= new Temp(); //创建实例 System.out.println(a.t); //通过实例访问实例变量 } }
The result is
0
1
0
The above is the detailed content of What is the difference between Java static variables and instance variables?. For more information, please follow other related articles on the PHP Chinese website!