Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic