aspose file tools*
The moose likes Beginning Java and the fly likes what are local, member, and class variables? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what are local, member, and class variables?" Watch "what are local, member, and class variables?" New topic
Author

what are local, member, and class variables?

Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
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


Vinod Kumar Nair
"Any fool can write code that a computer can understan. Good programmers write code that humans can understand."
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3270
    
  52

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.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

This is Java 101 so I'm moving this to our Beginning Java forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051


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


SCJP6.0,My blog Ranchers from Delhi
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

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.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
How about initialization blocks, static or non-static? They seem like
unnamed constructors. And are constructors considered methods?

Jim ... ...


BEE MBA PMP SCJP-6
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
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.
Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
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 ?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Thank you. I had forgotten about inner/nested classes.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
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 ... ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what are local, member, and class variables?