The term "class variable" isn't a very clear way to describe a variable or field in Java.
I had assumed you meant a variable (better called a "field") associated with the class itself, not with an instance of the class. Such variables are declared "static". An unambiguous way to describe one of these is "a static field of a class".
But the above example has "instance fields" - variables that are associated with an instance of the class. Such variables do not have "static" in their declaration.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
But the above example has "instance fields" - variables that are associated with an instance of the class. Such variables do not have "static" in their declaration.
Yup. But the whole point of the code snippet was to show the difference between the "count" (the local variable of the method) and the "age" the non-local (or class) variable. [ August 30, 2007: Message edited by: Maneesh Godbole ]
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
posted
0
The word to watch for when reading up on this kind of thing is "scope", that is how long does the variable live. A local variable exists only until the method returns. If you build up some temporary strings it's good to let them go out of scope when you return.
You can make an even shorter scope inside a pair of curly braces. If you declare a variable within a loop or if clause then it goes out of scope at the end brace.
If you build up a value that you'll need again later, losing it would be a bad thing. It would be better to make it a member field or static field. A member field scope matches the object instance; it's good until the object is no longer referenced. A static field scope is probably until you stop the JVM, though you could muck about with class loaders and get some sort of custom scope in controlled situations. [ August 30, 2007: Message edited by: Stan James ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Anand Divakaran
Greenhorn
Joined: Jul 20, 2007
Posts: 6
posted
0
class Example { int a ; //Instance Variable void methodexample { int b ; // Local Variable } }
Hope this example is simple enough
pras
Ranch Hand
Joined: Apr 04, 2007
Posts: 188
posted
0
i also know how to declare a class variable and local variable.
i want to know under what scenario we should declare a class variable and under what scenario we should declare a local variable?