Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Local Variable vs. Member Variable

 
Marius BONG
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the different between those two variable ? i got confused when i read some books which tell me different thing.
 
Bhushan Jawle
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Local variables have scope limited to the enclosing braces (ie. {,})
whereas member variables are always associated with an instance of a class and their scope is same as the scope of the enclosing object
e.g
class SomeClass
{
int x; // <- this is a member variable
void foo()
{
int y; // <- this is a local variable for function foo
}
}
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Member variables are part of the object that is build on the heap. So they are fairly permanent things, at least as permanent as the object is.
Local variables are convenience items used by Methods to do what they need to do. They are kept in the stack that relates to the method being executed. When the method is over - they die.
Static variables are part of the class definition, and are kept in the classfile. When a class is first referenced in any manner the classfile is built, along with the static variables, and stays there until the class is unloaded - which for the most part is as long as the JVM is running.
 
Marius BONG
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot guys...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic