What’s the difference between a class variable and an instance variable?
2011-11-16 16:40
399 查看
What’s the difference between a class variable and an instance variable?
between instance variables and class variables.
Instance variables
Instance variables belong to an instance of a class. Another way of saying that is instance variables belong to an object, since an object is an instance of a class. Every object has it’s own copy of the instance variables. Hereis what a declaration of an instance variable would look like:
Example of an instance variable:
class Taxes { int count; /*...*/ }
Class variables – also known as static member variables
Class variables, however, only have one copy of the variable(s) shared with all instances of the class. It’s important to remember that class variables are also known as static member variables inC++, Java, and C#. Each object of the class does not have its own copy of a class variable. Instead, every object shares the one and only copy of that class variable – and any changes made to that copy are seen by all of the objects of that
class. Here is what a class variable – or a static member variable – would look like in C++:
Example of a class variable:
class Taxes { static int count; /*...*/ }
Difference between class and instance variables
Now, it should be clear what the difference between instance and class variables is. Class variables only have one copy that is shared by all the different objects of a class, whereas every object has it’s own personal copy ofan instance variable. So, instance variables across different objects can have different values whereas class variables across different objects can have only one value.
Class and Instance variables are both Member variables
Here’s a little diagram to help you remember the differences between instance and class variables:相关文章推荐
- What’s the difference between an interface and an abstract class in Java?
- What is the Difference Between Type and Class in
- WHAT'S THE DIFFERENCE BETWEEN AN OEM AND AN ODM?
- In SQL, what’s the difference between a full join and an inner join?
- What is the difference between Class Path and Build Path
- What's the difference between tf.placeholder and tf.Variable
- What is the difference between using a script and an animation?
- What is the difference between an EXE and a DLL?
- What are the differences between struct and class in C++?
- What are the differences between a pointer variable and a reference variable in C++?
- What is the difference between @staticmethod and @classmethod in Python?
- What is the difference between NoClassDefFoundError and ClassNotFoundException?
- In C++, what’s the difference between an inline function and a macro?
- What are the differences between a pointer variable and a reference variable in C++?
- [置顶] What is the difference between Category and Class Extension?
- The difference between declaring a variable “id” and “NSObject *”
- What is the difference between GenericServlet, HttpServlet and a Servlet?
- What is the difference between procedural and nonprocedural language?
- What is the difference between CDATA and PCDATA?
- What is the difference between SSH and SSL?