1. Introduction:
1. A class variable is also called a static variable, that is, a variable with static in front of the variable;
2. The instance variable is also called an object variable, i.e. a variable without static;
The difference is:
The difference between a class variable and an instance variable is that the class variable is common to all objects, where one object changes its value, the other object gets the changed result, and the instance variable is the object's private, and an object changes its value without affecting other objects;
Cases:
1 Public classa{2 Static intA =0;//class Variables3 Private intb =0;//Strength Variable4 } 5 6 Public classb{7 Public voidMain (string[] args) {8A A1 =NewA ();9A A2 =NewA ();Tena1.a =3;//equivalent to A.A = 3; Onea1.b =4 ; AConsole.WriteLine (a2.a);//result is 3 - //The class variable is for all objects, so A1 change a,a2 a also changes -Console.WriteLine (a2.b);//result is 0 the //The instance only changes itself, so the B change of the A1 object does not affect the B variable of the object A2 - } - -}
A class variable is also called a static member variable, and it does not need to create an object to exist in memory.
Class is like a description of the specification of an instance object of this class. When creating an instance object, memory will open up a memory space for each non-static member variable of each instance object to store all non-static member variable values for that object. Even though two different instance objects belong to the same class class, their non-static member variables with the same name occupy a different amount of space in memory.
Static member variables are also different from class variables. All instance objects share a class variable, and there is only one space in memory where the class variable value is placed. Therefore, if an object changes the value of a class variable, the other object takes the value of the class variable again after it has been changed.
The difference between a class variable and an instance variable