Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming / Difference Between Class and Instance Variables

Difference Between Class and Instance Variables

February 5, 2018 Posted by Lithmee

Key Difference – Class vs Instance Variables
 

Most modern programming languages support Object Oriented Programming. An object contains data or attributes. An object has certain behaviors. They are known as methods. A program can be model using objects. A software is a collection of programs. Therefore, a software can be designed and implemented using objects. Objects are interacted using methods. Object-Oriented Programming improves code modularity and reusability. There should be a class to create objects. A class is a blueprint to create an object. Therefore, an object is an instance of a class. In programming, the data needed to be stored. Data is stored in memory locations. These memory locations are called variables. A member variable is a variable that is associated with a specific object. It is accessible for all its methods. There are two types of member variables that are class variables and instance variables. The key difference between class and instance variables is that, if there is only one copy of the variable shared with all instance of the class, those variables are called class variables and if each instance of the class has its own copy of the variable, then those variables are called instance variables.

CONTENTS

1. Overview and Key Difference
2. What are Class Variables
3. What are Instance Variables
4. Similarities Between Class and Instance Variables
5. Side by Side Comparison – Class vs Instance Variables in Tabular Form
6. Summary

What are Class Variables?

When there is only one copy of the variable shared with all instance of the class, those variables are called as class variables. Class variables are variables declared within the class outside any method. These variables contain the keyword static.These variables are associated with the class, not to the object.

Difference Between Class and Instance Variables

Figure 01:  Class Variables and Instance Variables

Refer the below piece of code with class variables.

public class Employee {

public static int id;

public static double salary;

}

public class Test {

public static void main(string[] args){

Employee e1= new Employee();

Employee e2= new Employee();

}

}

According to the above program, e1 and e2 are Employee type objects. Both will have the same copy of memory. If e1.id= 1 and printing e2.id will also give the value 1. It is possible to print the id and salary values using the Employee class name such as Employee.id, Employee.salary etc.

What are Instance Variables?

When each instance of the class has its own copy of the variable, then those variables are known as instance variables. Refer the below program.

public class Employee {

public int id;

public double salary;

}

public class Test{

public static void main(string[] args){

Employee e1= new Employee();

e1.id=1;

e1.salary= 20000;

Employee e2= new Employee();

e2.id =2;

e2. salary = 25000;

}

}

In the main program, e1 and e2 are references to the objects of type Employee. It is possible to assign values for id and salary using the dot operator such as e1.id, e1. salary etc. The id and salary in the class Employee are known as instance variables. The e1 and e2 are separate objects. Each object will have a separate copy of instance variables. The e1 will have separate id and salary and e2 will have a separate id and salary. So, the instance variables are created when the object or the instance is created.

What are the Similarities Between Class and Instance Variables?

  • Both are types of variables.
  • Both variables are inside a class but outside any method.

What is the Difference Between Class and Instance Variables?

Class Variables vs Instance Variables

Class variables are variables in which there is only one copy of the variable shared with all the instance of the class. Instance variables are variables when each instance of the class has its own copy of the variable.
 Association
Class variables are associated with the class. Instance variables are associated with objects.
Number of Copies
Class variables create one copy for all objects. Instance variables create separate copy for each object.
 Keywords
Class variables should have the static keyword. Instance variables do not require a special keyword such as static.

Summary – Class vs Instance Variables

Object-oriented programming is major programming paradigm. It helps to model a software using objects. Objects are created using classes. Object creation is also known as instantiation. A class provides a blueprint to create an object. A member variable is a variable that is associated with a specific object. It is accessible for all its methods. There are two types of member variables as, class variables and instance variables. The difference between class and instance variables is that, if there is only one copy of the variable shared with all instance of the class, those variables are called class variables and if each instance of the class has its own copy of the variable, then those variables are called instance variables.

Download the PDF Version of Class vs Instance Variables

You can download the PDF version of this article and use it for offline purposes as per citation note. Please download the PDF version here: Difference Between Class and Instance Variables

Reference:

1.tutorialspoint.com. “Java Object and Classes.”  The Point. Available here
2.“Instance variable.” Wikipedia, Wikimedia Foundation, 16 Dec. 2017. Available here   
3.“Class variable.” Wikipedia, Wikimedia Foundation, 16 Dec. 2017. Available here

Related posts:

Difference Between Agile and Scrum Difference Between Java and Oracle Difference Between DLL and LIB Difference Between Open Source and Proprietary SoftwareDifference Between Open Source and Proprietary Software Difference Between Parallel and Distributed ComputingDifference Between Parallel and Distributed Computing

Filed Under: Programming Tagged With: Class and Instance Variables Differences, Class and Instance Variables Similarities, Class Variables, Class Variables Association, Class Variables Copies, Class Variables Definition, Class Variables Keywords, Class vs Instance Variables, Compare Class and Instance Variables, Instance Variables, Instance Variables Association, Instance Variables Copies, Instance Variables Definition, Instance Variables Keywords

About the Author: Lithmee

Lithmee Mandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

Leave a Reply Cancel reply

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Conservation and Preservation

Difference Between Conservation and Preservation

Difference Between Monophasic and Biphasic Defibrillator

Difference Between Monophasic and Biphasic Defibrillator

Difference Between Dot Product and Cross Product

Difference Between Feline and Canine

Difference Between Janitor and Custodian

Latest Posts

  • Difference Between Camphor and Menthol
  • Difference Between Aminocaproic Acid and Tranexamic Acid
  • Difference Between Nitronium Nitrosonium and Nitrosyl
  • Difference Between Trichloroacetic Acid and Trifluoroacetic Acid
  • Difference Between Group I and Group II Introns
  • Difference Between Ion Channel and Ion Pump
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018 Difference Between. All rights reserved. Terms of Use and Privacy Policy: Legal.