GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • DSA Tutorial
  • Interview Questions
  • Quizzes
  • Must Do
  • Advanced DSA
  • System Design
  • Aptitude
  • Puzzles
  • Interview Corner
  • DSA Python
  • Logic Building Problems2 min read
  • Analysis of Algorithms1 min read
  • Array3 min read
  • String2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Difference between Instance Variable and Class Variable - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • DSA Tutorial
  • Interview Questions
  • Quizzes
  • Must Do
  • Advanced DSA
  • System Design
  • Aptitude
  • Puzzles
  • Interview Corner
  • DSA Python

Difference between Instance Variable and Class Variable

Last Updated : 23 Jul, 2025

Instance Variable: It is basically a class variable without a static modifier and is usually shared by all class instances. Across different objects, these variables can have different values. They are tied to a particular object instance of the class, therefore, the contents of an instance variable are totally independent of one object instance to others.

Example:

class Taxes  
{  
   int count;  
   /*...*/  
}  

Class Variable: It is basically a static variable that can be declared anywhere at class level with static. Across different objects, these variables can have only one value. These variables are not tied to any particular object of the class, therefore, can share across all objects of the class.  

Example:

class Taxes  
{  
   static int count;  
   /*...*/  
}  

Tabular difference between Instance and Class variable:

Instance Variable

Class Variable

It is a variable whose value is instance-specific and now shared among instances.  It is a variable that defines a specific attribute or property for a class.  
These variables cannot be shared between classes. Instead, they only belong to one specific class.  These variables can be shared between class and its subclasses. 
It usually reserves memory for data that the class needs.  It usually maintains a single shared value for all instances of class even if no instance object of the class exists.  
It is generally created when an instance of the class is created.  It is generally created when the program begins to execute.  
It normally retains values as long as the object exists.  It normally retains values until the program terminates.
It has many copies so every object has its own personal copy of the instance variable. It has only one copy of the class variable so it is shared among different objects of the class.  
It can be accessed directly by calling variable names inside the class.  It can be accessed by calling with the class name.  
These variables are declared without using the static keyword.  These variables are declared using the keyword static.  
Changes that are made to these variables through one object will not reflect in another object.  Changes that are made to these variables through one object will reflect in another object. 
Comment

Explore

    DSA Fundamentals
    • Logic Building Problems2 min read
    • Analysis of Algorithms1 min read
    Data Structures
    • Array3 min read
    • String2 min read
    • Hashing2 min read
    • Linked List3 min read
    • Stack2 min read
    • Queue1 min read
    • Tree2 min read
    • Graph3 min read
    Algorithms
    • Searching Algorithms2 min read
    • Sorting Algorithms3 min read
    • Introduction to Recursion15 min read
    • Greedy Algorithms3 min read
    • Graph Algorithms3 min read
    • Dynamic Programming2 min read
    • Bitwise Algorithms4 min read
    Advanced
    • Segment Tree2 min read
    • Binary Indexed Tree12 min read
    • Trie Data Structure15+ min read
    • Square Root (Sqrt) Decomposition Algorithm15+ min read
    Interview Preparation
    • Software Developer Interview Preparation2 min read
    • GFG 1602 min read
    • Coding Practice1 min read
    • POTD2 min read
    Courses
    • Placement 360 Course2 min read
    • DSA and System Design Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.