This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about K&B book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about K&B book" Watch "Question about K&B book" New topic
Author

Question about K&B book

Serena Zhou
Ranch Hand

Joined: Dec 13, 2003
Posts: 31
Hi, I have a question about K&B book:
page 499 the code right below the exam watch:

Shouldn't this result in compilation error?
Thanks!
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
You're correct. The compiler will freak out. Runnable is an interface, and I don't think you can create "subinterfaces" anonymously like we can anonymous classes.
I don't have the book laying around anymore so I can't tell you what the authors meant in the context. But what they might have been going for is something closer to:


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Serena Zhou
Ranch Hand

Joined: Dec 13, 2003
Posts: 31
Thanks. but if you have pay attention to that ";" after Runnable(), you will find out that it is not the case.
The author might mean something like
Runnable r = new MyRunnable();// MyRunnable is a class that implements Runnable.
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
True. If MyRunnable truly is a class that implements the Runnable interface the program would compile. Since we only instantiate classes, using "new" on an interface type will always result in a compilation error unless we are talking about an anonymous class.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8801
    
    5
Oops!
Serena you're right! It's amazing that on the 6th printing of this book there's still a few errors
It should be something like: Runnable r = new MyRunnable();
Thanks!


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
 
subject: Question about K&B book
 
Similar Threads
page# 616 question# 10
Question from threads.
Runnable confusion
Anonymous Inner Class
Anonymous class