Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

member vs. local

 
Cheryl Gray
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • 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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe an example will help illustrate what has been explained:

HTH
Layne
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic