Java-Class variable Vs Instance variable

Some body please tell me what is the difference between class variable and instance variable???

Questions by nitya123

Showing Answers 1 - 1 of 1 Answers

cri

  • Sep 9th, 2012
 

A class variable is a variable that is declared as static. This variable can be accessed even before the object of the class is created. i.e it would be available when the class is loaded. Where as instance variable is a variable that is declared at class level without the keyword static.

  Was this answer useful?  Yes

forjavajob

  • Sep 28th, 2012
 

A class variable which is declared as static is common to all the instances of the Class, i.e if you make any change in the class variable it applies to all the objects created for the class. In case of Instance variable- each object created for the class has its own instance variable copy.

  Was this answer useful?  Yes

arvindkore

  • Oct 8th, 2012
 

class variable is static variable is create when class structure is defined ,and other variables which are create and destroyed with object are called instance variables

  Was this answer useful?  Yes

qamrun

  • Nov 8th, 2012
 

Static variable, the field is allocated when the class is created. It belongs to the class and not any object of the class. It is class variable.

Instance variable, the field is allocated when the class is instantiated to the class is called instance variable or non-static variable

Static variable has only one copy for all the methods in class while instance variable has many copy.

Class can access only static variable while object can access both class and instance variable.

  Was this answer useful?  Yes

Class variable :: It is a variable which we can access this variable by using class name and another advantage is when the class loads in ClassLoader(i.e JVM) at that time itself the variable is initiated. Best examples of class variables are static variables.

Instance variable :: It is a object variable i.e when we create that object for that particular class then only we can access this varable i.e objectName.instanceVariableName.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions