• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

what are local, member, and class variables?

 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7386
1412
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
[email protected]
 
Marshal
Posts: 80133
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about initialization blocks, static or non-static? They seem like
unnamed constructors. And are constructors considered methods?

Jim ... ...
 
Rancher
Posts: 5093
82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 5093
82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80133
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ... ...
 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic