• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Anand Divakaran
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Example
{
int a ; //Instance Variable
void methodexample
{
int b ; // Local Variable
}
}

Hope this example is simple enough
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic