The difference between a class variable and an instance variable

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.