• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

what are local, member, and class variables?  RSS feed

 
Ranch Hand
Posts: 220
  • 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
 
Sheriff
Posts: 6002
936
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Likes 1
  • 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.
 
Sheriff
Posts: 21502
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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.
 
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
 
Marshal
Posts: 60803
190
  • 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
Posts: 21502
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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.
 
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 ... ...
 
Ranch Hand
Posts: 3109
18
  • 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: 220
  • 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: 3109
18
  • 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
Marshal
Posts: 60803
190
  • 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 ... ...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!