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
Explain how to create instance of a class - Java supports 3 ways of creating instances - By using new operator – Ex : Product prodMouse = new Product();....
What is an abstract class?
What is an abstract class? - A class with one of more abstract methods is called an Abstract class....
Java - Difference between an Abstract class and Interface
Difference between an Abstract class and Interface - An abstract class can have both abstract and concrete methods whereas an interface can have....
Post your comment