File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Instance Variables & Class Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instance Variables & Class Variables" Watch "Instance Variables & Class Variables" New topic
Author

Instance Variables & Class Variables

Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
Hi,
I want to know the difference between Instance Varible and Class Variables(Static Variables)?
When We create instance of class, what is the diffference between Static Variables and Instance variables?
Thanks
Angela
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Angela
The quick answer is:
instance (member) variables are variables that belong to each iondiovidual instance of an object. Each instance has its own copy of the variable.
static (class) variables belong to the class itself there is only one copy of the variable no matter how many of instances of the class there are.
This can be a fairly detailed topic and you can find other resources for it:
the java tutorial is probably the place to start.
hope that helps


Dave
Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
Thanks Dave,
So,If I have class bicycle:
In bicycle, if i declare Static Variable currentGear = 45.If I create 2 instance of this class, bicycle a, bicycle b and in a if can I change the the value of currentGear? If yes, then the value of currentGear in b and class itself will be changed or not???
same question for instance variable???
BTW, I read the java tutorial, still I have above confusion.
I would appreciate answer(If it is by simple example)
Thanks
Angela
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
Originally posted by Angela D'souza:
So,If I have class bicycle:
In bicycle, if i declare Static Variable currentGear = 45.If I create 2 instance of this class, bicycle a, bicycle b and in a if can I change the the value of currentGear? If yes, then the value of currentGear in b and class itself will be changed or not???
same question for instance variable???




Notice the output after each change. Now remove the "static" modifier from currentGear, and see how the output differs. (Note: You will need to comment out this line:
System.out.println( "Bicycle.currentGear: " + Bicycle.currentGear );
or it won't compile.)


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instance Variables & Class Variables