This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "variables" Watch "variables" New topic
Author

variables

aliya az
Greenhorn

Joined: Sep 06, 2005
Posts: 1
what is the main difference b/w class variables and instance variables??


Thanks in advance!!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Welcome to JavaRanch Aliya.

A class variable (for example: static int var1) is shared between all instances (objects) of the class. If a method updates a class variable all the other objects of the class will see the the one changed value.

An instance variable (for example:int var2) is unique to each instance of the class. If an object updates its instance variable it does not affect the values of the instance variable in the other objects of the class.

Please, in future, consider asking such basic questions in our Java In General (Beginner) forum.
Thanks
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
anand phulwani
Ranch Hand

Joined: Sep 10, 2005
Posts: 242
You can take it like this,

1)Class Variables:- these are variables marked as static,they are binded to the class itself thats why they are called class variables,these variables can be accessed by two method classname.variable,object.variable,but for all of them there exist only a single instance of class variable,moreover these variables are automatically initialized at class loadtime and implicitly given a default value of that primitive.
here is code to help you out and will clear our doubts

Results into
0
15
10

2)Instance Variables:- They are binded to the particular instance of class itself thats why they are called instance variables,these variables can be accessed by object.variable,for all of instances there exist a different instance variable,these variables are automatically initialized at object creation and implicitly given a default value of that primitive.
here is code to help you out and will clear our doubts

which results into
0
15
[ September 17, 2005: Message edited by: anand phulwani ]

Thanks and Regards, Anand
SCJP 5.0 310-055 73%, SCWCD 1.4 310-081 78%, IBM DB2 9 Fundamentals 000-730 62%
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Anand's post reminded me of something else. It is strongly recommended not to refer to a class variable through an object instance. That is, as obj.myClassVariable. It is recommended that you either use myClassVariable (in the defining class) or MyClass.myClassVariable (where MyClass is the class where myClassVariable is defined). The whole point of class variables is that you do not need an instance of MyClass to be able to reference them.


(Strange but true: you can, but should not, use ((MyClass)null).myClassVariable or MyClass ref = null; ref.myClassVariable to access a class variable )
anand phulwani
Ranch Hand

Joined: Sep 10, 2005
Posts: 242
Dear MR Barry,

With refernce to your above post,i would like to know what should we do in the exam when a question regarding this that a class variable is accessed by an object reference comes.

what i think is although sun advices we shouldnt do this ,but related to exam,i think we have to see what runs and what not,hope i am right and you would agree with me.

Thanks,
With Regds,
Anand
 
 
subject: variables
 
Similar Threads
Why Java ?
Opening .bat files from ant
Variables in Java
Runtime.getRuntime() help plzzzzzzzz
Variables