Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between instance variable and class variable

 
Divya Gehlot
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can Any one explain me difference between instance variable and class variable.As i got confused with those.
Thanks in advance.
 
Ameen khan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ August 07, 2007: Message edited by: Ameen khan ]
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15437
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic