File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes threads.....i thought a compile err would occur Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "threads.....i thought a compile err would occur" Watch "threads.....i thought a compile err would occur" New topic
Author

threads.....i thought a compile err would occur

srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
Hi, i thought a compile err would occur,but it didnt,and output produced is nothing.......im confused,can anybody pls explain this....

public class background extends Thread{
public static void main(String argv[]){
background b = new background();
b.run();
}
public void start(){
for (int i = 0; i <10; i++){
System.out.println("Value of i = " + i);
}
}
}

please use the [code][/code] tags when showing code. visit <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page</a> ,for more details
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
No compile error occurs because you override the start(), and then you call the run(). Since you didn't override run(), it does nothing as it would by default.
srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
Hi sean, im still not clear, can u explain in more broad way
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
Normally, if you want a thread to have functionality, you extend Class Thread, override the run(), and then create an instance and call start(). By default, the run() of class Thread is empty, so if you don't override it, then you will inherit it, and as I pointed out it is empty. IN your example, the start method is overridden, but the run method isn't. So when b.run() is called it does nothing. Note: You usually wouldn't override the start(), but it is perfectly legal.
In order to cover my tracks on my first statement: Of course you could also implement Runnable whereas you'd be required to override the run().
[This message has been edited by Sean Casey (edited July 03, 2001).]
srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
thanks sean i got it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: threads.....i thought a compile err would occur
 
Similar Threads
array...why does it give err at run time, .....!!!
The same old Interface reference type issue
Doubt in overloading
Clone Method
implicit downcast possible?