What’s the difference between a class variable and an instance variable?




Knowing the terminology is important. Instance variables and class variables are both member variables. They are both member variables because they are both associated with a specific class. But, there are differences 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. Here is 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 in C++, 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 of an 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:

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.

  • Partha Krishna

    nice explanation in simple terms

  • preet

    thanks

  • srinivasa rao Yamalapalli

    This is good article for the instance and class variables difference 🙂

  • Prabha

    Great explanation,,,,Thank you so much

  • Anonymous

    In a game such as Five Nights at Freddies class variables will be used. Your total power source is shared by all of your in game activities such as closing the doors, using the voice, turning on and off lights, etc. Each one of these does not have it’s own individual power source.

  • Leandro Gecozo

    Wow nice and direct explanation!

  • rakesh

    wow explanation 🙂

  • SAM

    Very disgusting answer

  • Aditya Naik

    Thanks man! 🙂

  • Ashish Sikarwar

    daigram is right.
    And here explanation method is simple

  • krishna

    thanks 🙂

  • Jack

    Thanks for the explanation, especially the diagram, I do understand it better now.

  • Tran Khanh

    Now I can clearly understand those confused concept. Thanks a lot.

  • nikhil

    nice
    one

  • anil

    This definitely not in python 🙂

  • anil

    Good and simple explanation

  • Lal krishna

    y we dont?..can u explain

  • gulshan

    usually we dont write
    class variables

  • Nikhil

    thanks!

  • shamim

    good use of static variable as a name class variable

  • Derek

    Thank you! I code in COBOL at work, so Object Oriented programming isn’t my strength. This was exactly what I was looking for, thank you!

  • laka

    nice one !!

  • sri

    Good One..

  • anon

    Good explaination