File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes difference between instance variable and class variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "difference between instance variable and class variable" Watch "difference between instance variable and class variable" New topic
Author

difference between instance variable and class variable

Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
Hi,
Can Any one explain me difference between instance variable and class variable.As i got confused with those.
Thanks in advance.


SCJP1.5(81%), SCDJWS(94%), next mission SCEA (but need to wait or that)
Ameen khan
Ranch Hand

Joined: Jun 10, 2007
Posts: 52

[ August 07, 2007: Message edited by: Ameen khan ]

SCJP 5.0<br />Next-> I Don't Know
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
Originally posted by Ameen khan:
Class variables = instance variables + static variables




When you create different instances of a class, each instance will have it's own copy of the (instance) variables defined in the class. Modifying an instance variable of one class won't affect the instance variable of another class.
When you define a variable as static, it becomes a class variable. In this case only one copy of the variable is made and all the instances will share this single copy. If you make a change to this variable, it will reflect everywhere (i.e. in all instances).


Mani
Quaerendo Invenietis
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

See this: Understanding Instance and Class Members

Instance variable: For every instace that you create of a class, there is a separate copy of the variable - each object has its own copy of the variable.

Class variable: There is one variable which is shared by all the instances of the class - there's only one variable which is shared by all the objects.

Class variables are member variables that have the static modifier. Instance variables are member variables that do not have the static modifier.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
srinivas sridaragaddi
Ranch Hand

Joined: Jul 24, 2007
Posts: 225
hi i found the difference, its not very detailed but basic difference is given and the link is
http://www.sap-img.com/java/difference-of-class-variable-and-instance-variable.htm


SCJP 5.0<br /> <br />"Skills are started with learning and mastered with improvement. Nothing is hereditary except death" BUDDHA...
srinivas sridaragaddi
Ranch Hand

Joined: Jul 24, 2007
Posts: 225
hi i found the difference, its not very detailed but basic difference is given and the link is
http://www.sap-img.com/java/difference-of-class-variable-and-instance-variable.htm
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between instance variable and class variable