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

what are local, member, and class variables?

 
Vinod Vinu
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4690
336
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is Java 101 so I'm moving this to our Beginning Java forum.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about initialization blocks, static or non-static? They seem like
unnamed constructors. And are constructors considered methods?

Jim ... ...
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I had forgotten about inner/nested classes.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic