File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes accessing static members via reference variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "accessing static members via reference variables" Watch "accessing static members via reference variables" New topic
Author

accessing static members via reference variables

Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
This is from Bates/Sierra Java certification book:


"Accessing Static Methods and Variables

Since you don't need to have an instance in order to invoke a static method or access a static variable, then how do you invoke or use a static member? What's the syntax? We know that with a regular old instance method, you use the dot operator on a reference to an instance:




In the preceding code, we instantiate a Frog, assign it to the reference variable f, and then use that f reference to invoke a method on the Frog instance we just created. In other words, the getFrogsize() method is being invoked on a specific Frog object on the heap.

But this approach (using a reference to an object) isn't appropriate for accessing a static method, because there might not be any instances of the class at all! So, the way we access a static method (or static variable) is to use the dot operator on the class name, as opposed to using it on a reference to an instance, as follows:



In the preceding code, we instantiate a Frog, assign the new Frog object to the reference variable f, and then use the f reference to invoke a static method! But even though we are using a specific Frog instance to access the static method, the rules haven't changed. This is merely a syntax trick to let you use an object reference variable (but not the object it refers to) to get to a static method or variable, but the static member is still unaware of the particular instance used to invoke the static member. In the Frog example, the compiler knows that, the reference variable f is of type Frog, and so the Frog class static method is run with no awareness or concern for the Frog instance at the other end of the f reference. In other words, the compiler cares only that reference variable f is declared as type Frog. Figure 2-8 illustrates the effects of the static modifier on methods and variables."

First they say it is not appropriate to access a static member from a reference variable, and then they say (to make things more confusing) that the Java language also allows you to use an object reference variable to access a static member. Can someone clarify this double talk or explain what they are saying???
Virendrasinh Gohil
Ranch Hand

Joined: Jun 09, 2004
Posts: 46
I know. Enough confusing.

Just to answer you quickly, when any static member is access it is always called using class and not reference variable. However, compiler wont shout at you if you call the static member (variable or method) using object of that class. Compiler is smart enough to resolve the type of the class to call the method. I know it is redundant to use class object to call static variable but syntactically it is allowed.

You can read more about this at http://www.coderanch.com/t/506243/java/java/static.

I missed the link of one of the thread in JavaRanch on static method overriding discussion sometime back. It explained the concept in great detail. If anybody remembers that link, please post it here.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Vonique Leary wrote:
First they say it is not appropriate to access a static member from a reference variable, and then they say (to make things more confusing) that the Java language also allows you to use an object reference variable to access a static member. Can someone clarify this double talk or explain what they are saying???

No! I dont think any book would have said that.
Static members belong to the Class itself instead of the instances created. That is, it is same & common to all the instances - that is exactly why you dont actually need instances to access them. Even if you are going to access with an instance of a Class, the compiler is going to see only the reference associated with it and the object never matters at all.


OCPJP 6
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Virendrasinh, thanks for your comments and the link to the article. Very interesting.


"No! I don't think any book would have said that. "
Vinoth, it doesn't matter what you may "think" because that is a direct quote. The book is called Sun Certified Programmer for Java 5: A Study Guide. Check your facts first.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@Vonique: please mind your tone and keep things friendly. People have different experiences, different languages, and different interpretations.

One reason it's considered "inappropriate" is because it's misleading: if you access a static variable through an object reference the *implication* is that you're accessing a member variable (property) and not a static. This is more to do with convention than anything else--the "proper" way to access a static member is with the class name syntax. So:Just because something is inappropriate doesn't mean it isn't possible--just that it should be avoided.
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Well, that was the answer I was looking for, because I was confused when they said it was inappropriate but then gave us an example of that very same inappropriate code as something that can be done. So, it is possible but not the best thing to do.

Thanks!

Sorry I sounded snippy but I felt like he was saying I didn't know what I was talking about but maybe he was right. My apologies!

Vonique
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

He just didn't believe a book would say that (although I'm not sure why not).

There's all sorts of inappropriate ways we can do things--and we need to know what they are, and how to do them, in order to (a) avoid then, and hopefully (b) understand *why* and how to avoid them!
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

David Newton wrote:He just didn't believe a book would say that (although I'm not sure why not).

I had it interpreted wrong. I just took 'it is not possible to access a static member from a reference variable'. I must have also added 'I'm not sure though.' Without this I might have sounded rude you. My apologies, Vonique.
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
No problem, Vinoth. I guess we both miscommunicated. Anyway, I appreciate that you tried to help me! Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: accessing static members via reference variables