• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Typo on page 279 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Raluca Dragoi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first paragraph :

The method getJumpHeight() works just like a static method as defined in a class. In other
words, it can be accessed without an instance of the class using the Hop.getJumpHeight() syn-
tax.



Shouldn't it be?
The method getJumpHeight() works just like a static method as defined in a class. In other
words, it cannot be accessed without an instance of the class using the Hop.getJumpHeight() syn-
tax.


 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A static method can be accessed without an instance of a class.

The reason for this is that a static method is associated with a class. It is not associated with an instance of a class.
 
Raluca Dragoi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe . Thanks for the quick reply. I agree with you.
I was actually thinking that this might be a typo in case of accessing method getJumpHeight() -without an instance of the class - in a class implementing the interface Hop (there is an example below this paragraph that does not compile). That's why I got confused.

I think that the paragraph is also available for the case:
accessing a static method defined in an interface (without using an instance of the class), in a default method (defined in an interface).

Or is there any other case in which a static method (defined in an interface) could be accessed directly?

 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code does not compile because it's missing a curly bracket }

If you fix this problem, you can call Hop.getJumpHeight() directly, even without defining a class that implements Hop. You can do this because getJumpHeight() is a static method.

The same cannot be said for a default method. In order to call getSpeed(), you must define a class that implements Hop (and override getSpeed() if you please). You can then instantiate the class and call getSpeed().
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raluca Dragoi wrote:Shouldn't it be?
The method getJumpHeight() works just like a static method as defined in a class. In other
words, it cannot be accessed without an instance of the class using the Hop.getJumpHeight() syn-
tax.

No, it shouldn't. The study guide is spot-on! A static interface method is lika a class (static) method: you don't need any instance (of a class) to invoke this method.Note that this only applies to static interface methods and not to default nor abstract interface methods.

Another important thing to note: when you try to access (invoke) a static interface method outside the interface, you always have to use an explicit reference to the name of the interface. Otherwise the code will not compile! And again: it doesn't matter if the class implements this interface or another interface inherits from this interface, you must explicitly use a reference to the name of the interface. All these scenarios are illustrated in this code snippetSo trying to invoke the static interface method fly() outside the interface itself without an explicit reference to the name of the interface will result in a compiler error (CE1). When you try to invoke the default interface method show() without an instance of a class (implementing the Flyable interface) will give a compiler error too (CE2).

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic