File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes java (beginner) 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 "java (beginner)" Watch "java (beginner)" New topic

java (beginner)


Joined: Feb 28, 2001
Posts: 1
what is a class variable and Instance variable ?
Fawad Khan
Ranch Hand

Joined: Feb 13, 2001
Posts: 36
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.
// Variables declared here ( in the braces of a //method) are known // as local( to the method //where they are ) variables .

Seeking Certification
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
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.
Helmut Lerch
Ranch Hand

Joined: Feb 11, 2001
Posts: 48
Originally posted by PadmajaSudhir:
what is a class variable and Instance variable ?

Perhaps you take a look at
I agree. Here's the link:
subject: java (beginner)
It's not a secret anymore!