[Logo]
Forums Register Login
what are local, member, and class variables?
hi please explain me with e.g. the difference between local,member, instance variables and class variables in java?


what i know is that,
local variables - inside methods only (may or may not using 'static' keyword)
instance varibles - declared inside method(without using 'static' keyword) and belonging to Objects created from Class
class variables - declared inside class and not within any method, available to all the methods inside the class.
member variable - DON'T KNOW, Please tell me

please explain me if iam wrong anywhere.

thank you
(1 like)
local variables - inside methods only. Cannot use any keyword other than 'final'.
instance varibles - Non-static variables declared inside a class, but outside a method.
class variables - Static variables declared inside a class, but outside a method.
member variable - Both instance variables and class variables are generally called as member variables.
This is Java 101 so I'm moving this to our Beginning Java forum.

what i know is that,
local variables - inside methods only never used any static keyword......they are not initialized if you donot assign value to them then compiler error throws
instance varibles - declared inside class........they are by default initialized to their default value like 0 for int..and they can have different value for each method
class variables - when we used static keyword with any of the above variable then it will become a class variable..becoz its value will be same for the entire class......even if you change its value it new value will be assign to everywhere where it appers
member variable --its is the different name of instance variable,field,...field is a preferred name in java but member is a preferred name in c++


anything else wanna know......ask me
on my email id
sweetshankysohar@gmail.com
Member is a generic term in Java for fields and methods. So those members which are variables are called fields, and those members which aren't are called methods. I think (not sure) that static fields and methods are not "members" of an object.
Don't forget nested classes; those are members as well.

According to section 8.1.6 (scroll down a bit), the members of a class are all fields, methods, nested classes and nested interfaces. There is no distinction between static or not.
How about initialization blocks, static or non-static? They seem like
unnamed constructors. And are constructors considered methods?

Jim ... ...
No. As specified in the link given (JLS 8.1.6), members are fields, methods, classes, and interfaces declared in the body of a class. And in the JLS, a constructor is never referred to as a method. They are similar but different, and the JLS maintains separate terms for each.
two statements given by two different authors are contradicting with eachother:-

member variable - Both instance variables and class variables are generally called as member variables.

by Devaka Cooray


member variable --its is the different name of instance variable,field,...field is a preferred name in java but member is a preferred name in c++

by shanky sohar


Which one is correct ?
Devaka is correct. Rob Prime already answered this, and gave a quote from the Java Language Specification as evidence. Static and non-static fields are considered member fields.
Thank you. I had forgotten about inner/nested classes.
So it turns out that some of the "stuff" defined within a class, specifically,
initialization blocks and constructors, are not considered to be members
of the class. They're just other class "stuff".

Jim ... ...

This thread has been viewed 6512 times.

All times above are in ranch (not your local) time.
The current ranch time is
Dec 11, 2018 10:39:00.