• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

member vs. local

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference between a variable being a member or being local?
This is what I understand:
class A
{ int aa; //here aa is a member
}
In what context would aa be local?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A variable is considered 'local' when it is declared within the body of a method.
A member (or instance) variable is "in scope" for the entire duration of the object instance. Whereas a local variable will be in scope only from the point of declaration to the end of the block within which it is declared.
Be sure to ask for clarification if that doesn't clear things up for you.
hth,
bear
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe an example will help illustrate what has been explained:

HTH
Layne
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A member variable, aka instance variable, aka field is part of the object that is created on the heap. There is a place inside that object to track the current value of the reference that the variable holds. It stays there until the object itself is no longer referenced by anything, at which time it is rounded up by the gc.
A local variable, aka automatic variable is not part of the object. It is a variable that is created automatically when a method or block of code is executed that declares the variable, and lives on the stack that is associated with the method. When the method is over, the stack for that method is rudely cast aside and thus the local variable is destroyed.
You might enjoy reading Not all Variables are created Equal.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic