• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is a member variable  RSS feed

 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read that "A member variable of a class is created when the instance is created." (The Complete Java Certification Study Guide, Simon Roberts et.al., pg 15).
and
"In the class body are the declarations of the members of the class. These include fields(variables), methods, static initializers, and constructors." (Exam Cram, pg 61)
Now!
In reference to variables, are member variables also static(no instance required) or non-static(a instance is required)?
Should I say these are just the declarations for possible instance variables of a class?
I guess I don't have a clear grasp on what part of a source file is member/or not?
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A member variable is a member of the instance.

In the code above, m_numMonkeys and m_monkeyNames are member variables. (I like to use a coding convention which prefixes member variables with m_; this helps to distinguish them from local variables, often with similar names.) Each instance of the class has its own version of those variables.
The static variable is not a member variable. It is a single field shared by all instances of the class. (In my coding convention, I use s_ for static variables.)
The static variable is created (i.e. in scope) when the class is loaded. The member variables aren't valid until a new instance is created, and then it is only valid within the instance. These member variables (m_ ones) are not static and cannot by referenced from the class itself, the way a static field can be.
Apparently the constructor is also a member (although I'm too rusty to remember details about this, but I'll trust the book). It seems wierd that the static initializer would be given that static fields aren't. But again, I'm rusty on the details.


--Mark
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read that "A member variable of a class is created when the instance is created."
The book is wrong here, according to the Java Language Specification. A member can be any field, method, nested class, or nested interface. Looking at just the first of these, a field can be either a static field (also called class variable), or a non-static field (also called instance variable). If it's a static field, it is not created when an instance is created; it was created when the class was loaded.
"In the class body are the declarations of the members of the class. These include fields(variables), methods, static initializers, and constructors." (Exam Cram, pg 61)
This is also wrong according to the JLS. Static initializers and constructors are not members.
I like to use a coding convention which prefixes member variables with m_; this helps to distinguish them from local variables, often with similar names.
Ewww. You're one of them. (See discussion here.) You'd probably bring in Hungarian notation too, if given the chance. :roll: Begone, servant of darkness! Tempt us not with your evil ways!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!