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.