Differences between class variables, instance variables, class methods, and instance methods in Java
Class variable: Such as static int a. As the name suggests, class variables can be understood as class variables. class variables have allocated memory space when they are loaded, unlike instance variables (int a;), instance variables are allocated memory when the class creates an object. In addition, every time an object is created, the corresponding memory space will be allocated to the instance variable, and the class variable does not belong to any object and is shared by all objects, class variables of all objects occupy the same memory space. You can useClass Name. Class variableYes.Object Name. Class variable. Class variables are the same as static variables exposed in C/C ++ and have the memory function.
Class Method: static void show (); similar to class variables, class methods also belong to the class. When the class is loaded, an entry address is assigned to the class method, and the instance method, the entry address is allocated only after the object is created. Similarly, class methods can be called by all pairs.Class Name. Class MethodOrObject Name. Class Method. Instance methods can only be called by object names. Note that the class method cannot call instance variables because the class method is allocated when the class is loaded, you may not have created an object to allocate memory space for instance variables, that is, instance variables may not exist, so you cannot use class methods to call instance variables. The instance method can call all instance variables and class variables.
Both instance variables and instance methods release the memory space only when the object disappears. class variables and class methods release all the memory space until the program ends and exits.