Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java (beginner)

 
PadmajaSudhir
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is a class variable and Instance variable ?
 
Fawad Khan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sudhir,
Look, every variable is different from other on the basis of its scope and access. Instance and class variables are the same. See,
class XYZ{
// Variables declared here (after class braces and not in any // method braces) are called instance or class variables.
myMethod(){
// Variables declared here ( in the braces of a //method) are known // as local( to the method //where they are ) variables .
}
}
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a little more to it than that Fawad. Class level variables are static, meaning that if there are 20 instances of a class, they all share that variable. So if object A changes a class variable to 20, then it will be 20 when you reference it from object B as well.
Instance variables belong to an instance of a class. Every object holds it own value. So if object A changes its instance variable to 20, object B will not have its instance variable changed also.
The third type is local variables, and those are variables that are inside of methods.
Bill
 
Helmut Lerch
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by PadmajaSudhir:
what is a class variable and Instance variable ?

Perhaps you take a look at http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic