This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes difference between a class variable and local method variable with an example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "difference between a class variable and local method variable with an example" Watch "difference between a class variable and local method variable with an example" New topic
Author

difference between a class variable and local method variable with an example

pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
can anyone help me in this regard.

when should we declare a variable local and when should we declare it as a class variable.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10491
    
    9

Originally posted by prasanna sheregar:
can anyone help me in this regard.

when should we declare a variable local and when should we declare it as a class variable.




Please read the code comments. They are self explanatory


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10491
    
    9

Originally posted by Peter Chase:


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
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
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
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between a class variable and local method variable with an example