• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Momen Travolta
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 433
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic