my dog learned polymorphism
The moose likes Beginning Java and the fly likes Local Variable vs. Member Variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Local Variable vs. Member Variable " Watch "Local Variable vs. Member Variable " New topic

Local Variable vs. Member Variable

Marius BONG

Joined: Dec 16, 2001
Posts: 13
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

Joined: Nov 22, 2001
Posts: 249
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
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"

Joined: Sep 29, 2000
Posts: 8521
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.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Marius BONG

Joined: Dec 16, 2001
Posts: 13
thanks a lot guys...
I agree. Here's the link:
subject: Local Variable vs. Member Variable
It's not a secret anymore!