my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes thread question in k&b's book for 1.5 (3) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "thread question in k&b Watch "thread question in k&b New topic

thread question in k&b's book for 1.5 (3)

Anna Chu
Ranch Hand

Joined: May 18, 2006
Posts: 30
The 17th question in chapter 9 self test:
public class TwoThreads {
static Thread laurel, hardy;
public static void main(String[] args) {
laurel = new Thread() {
public void run() {
try {
} catch (Exception e) {
hardy = new Thread() {
public void run() {
try {
} catch (Exception e) {

I think the code cannot compile because "hardy.sleep(1000)" is not correct since sleep() is a static function of Thread class, it should not be invoked by an instance.
Surendra Kumar
Ranch Hand

Joined: Jul 04, 2006
Posts: 235
Static methods can be called using class name or an instance reference.
But the latter is not good practice.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: thread question in k&b's book for 1.5 (3)
Similar Threads
New question on K&B Chapter 9 Question 17
Doubt in Threads
Query on the behavior of Thread sleep
Thread question from K&B book
How does this work?.Please explain