This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
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
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply 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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Do I have to use class name instead of instance name when trying to access a static member?
 
Similar Threads
Question about packages.
cannot resolve symbol
Storing objects polymorphically in a Figure array
clone() method in object class
Inheritance