aspose file tools*
The moose likes Beginning Java and the fly likes Accessing Static Member Using Object Reference Variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing Static Member Using Object Reference Variable" Watch "Accessing Static Member Using Object Reference Variable" New topic
Author

Accessing Static Member Using Object Reference Variable

Rishi Ugersain Chopra
Ranch Hand

Joined: Aug 28, 2004
Posts: 39

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 ]

Rishi Chopra<br /><a href="http://www.rishichopra.org" target="_blank" rel="nofollow">www.rishichopra.org</a>
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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:


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Mar 13, 2004
Posts: 1272
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".


Mike Gershman
SCJP 1.4, SCWCD in process
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Aug 28, 2004
Posts: 39

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

Joined: Aug 28, 2004
Posts: 39

BTW, I also sent an email to Kathy and Bert =)
Rishi Ugersain Chopra
Ranch Hand

Joined: Aug 28, 2004
Posts: 39

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing Static Member Using Object Reference Variable