Difference between instance variable and a class variable

Explain the difference between instance variable and a class variable.

An instance variable is a variable which has one copy per object / instance. That means every object will have one copy of it.

A class variable is a variable which has one copy per class. The class variables will not have a copy in the object.

Example :
class Employee
{
   int empNo;
   string empName,department;
   double salary;
   static int officePhone;
}

An object referred by empVismay is created by using the following:
Employee empVismay = new Employee();

Employee empSadhya = new Employee();

The objects referred by instance variables empVismay and empSadhya have separate copy of instance variables – empNo, empName, department and salary where as the officePhone belongs to the class and can be accessed as Employee.officePhone.
Explain how to create instance of a class by giving an example
How to create instance of a class - Java supports 3 ways of creating instances...
Define an abstract class. Explain its purpose
Define an abstract class - A class with one of more abstract methods is called an Abstract class...
What is the difference between an Abstract class and Interface?
Abstract class and Interface - An abstract class can have both abstract and concrete methods whereas an interface can have only method signatures...
Post your comment
Discussion Board
JAVA
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.
Partha Pratim mahata 10-9-2014