Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing Static Member Using Object Reference Variable

 
Rishi Ugersain Chopra
Ranch Hand
Posts: 39
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sierra and Bates (310-035 Exam Study Guide) mentions accessing static members using object reference variables. The example given uses this class definition:

class Frog {
static int frogCount = 0;
public Frog () {
frogCount += 1;
}
}

and uses the following code to access 'frogCount':

Frog f = new Frog();
int frogs = f.FrogCount();


Is this legal? The errata for the book uses 'f.getFrogCount()' so I have two questions:
(1) do I need another (static) method in the Frog class to access the static member and
(2) if I don't explicitly need the additional static method, should
(a) the member be accessed by prepending 'get' and
(b) should the member field be suffixed with '()'?

Thanks

[ October 17, 2004: Message edited by: Rishi Ugersain Chopra ]
[ October 17, 2004: Message edited by: Rishi Ugersain Chopra ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A static member is normally accessed with the something like ClassName.memberName. So for this example Frog.frogCount would be used.

You can call a "getter" method what you like. It's only a convention to have a method:


Obviously "setter" and "getter" methods are conveniently called setFrogCount and getFrogCount respectively. But you could also use the two methods:
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, it is possible in some cases to access a static member via an object reference. But it is not considered to be good practice. If an instance member variable is added to the class it will hide the static member when it is accessed in that manner. Some IDEs (Eclipse for example) will flag a warning if a static variable is accessed that way, but the standard Java compiler will not give any warning.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The erratum is in error. getFrogCount() isn't even defined in the example. Perhaps Kathy would like to comment on this.


The code you posted has two errors:

You defined the Frog object as g and then accessed it as f. This was your mistake transcribing from the book.

The () in f.FrogCount() shouldn't be there. That error is in the book With that fixed, the code works.

The corrected code compiles OK:


followed by



If an instance member variable is added to the class it will hide the static member when it is accessed in that manner.


Are you sure about that? I tried putting in static and instance variables of the same name using Java 5 and the compiler said "already defined".
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike said:

Are you sure about that? I tried putting in static and instance variables of the same name using Java 5 and the compiler said "already defined".


Nope.

What I really had in mind was a subclass redefining the variable:


Comment out the definition of x in the subclass, and the x value printed will be the static instance. Accessing x through ii is rather misleading because you may think it's an inherited instance member of the superclass object.

My bad, Mike, I didn't make myself clear enough (if I was in my own head, that is).
 
Rishi Ugersain Chopra
Ranch Hand
Posts: 39
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the comments; I was aware of the issues that Barry raised (accessing variables in such a manner not being good practice and the issue with subclasses.) I also fixed my f/g typo.

From what I understand then (correct me if I'm wrong) the book and errata both have typos; either the errata's version is correct and there should be a helper method (not so likely given the context) or the book's version is incorrect and the 'f.FrogCount()' statement should not include the parenthesis and capitalization should be corrected as appropriate.
[ October 17, 2004: Message edited by: Rishi Ugersain Chopra ]
 
Rishi Ugersain Chopra
Ranch Hand
Posts: 39
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, I also sent an email to Kathy and Bert =)
 
Rishi Ugersain Chopra
Ranch Hand
Posts: 39
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rishi Ugersain Chopra:
BTW, I also sent an email to Kathy and Bert =)


Looks like Kathy and Bert (1) do not check their PMs on this board and (2) don't respond to their published email addresses.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic