aspose file tools*
The moose likes Beginning Java and the fly likes Do I have to use class name instead of instance name when trying to access a static member? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Do I have to use class name instead of instance name when trying to access a static member?" Watch "Do I have to use class name instead of instance name when trying to access a static member?" New topic
Author

Do I have to use class name instead of instance name when trying to access a static member?

Momen Travolta
Greenhorn

Joined: Mar 19, 2010
Posts: 8
I wanted to try an example of my book, here is the code:-



In a second file:-


I got this message after trying to compile to the code (using NetBeans)
:-

Joachim Rohde
Ranch Hand

Joined: Nov 27, 2006
Posts: 423

You don't have to, but you should:
Class variables are referenced by the class name itself, as in

Bicycle.numberOfBicycles

This makes it clear that they are class variables.

Note: You can also refer to static fields with an object reference like

myBike.numberOfBicycles

but this is discouraged because it does not make it clear that they are class variables.

(source: http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Do I have to use class name instead of instance name when trying to access a static member?