File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads 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 "Threads" Watch "Threads" New topic


Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
Hi Every1,
I made these two programs for extends the Thread class and the other implements the Runnable interface.

Both the programs work fine but theres 1 thing I could not understand :

Program-1 prints the name of the thread as ThreadClass while Program-2 prints java.lang.Thread. I was expecting it to print RunnableThreadClass !!

Why's that so ?

Please help. What should I do so that it prints RunnableThreadClass ?


<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
Arul Prasad
Ranch Hand

Joined: Jan 20, 2005
Posts: 57
hi getClass() is the method of Object class which returns the class name
in ur second prog u r saying

here object t is belongs to Thread class thats why ur getting the java.lang.Thread.and the other point is ur RunnableThreadClass was implementing the Runnable interface it is not a Type of Thread ....

in your first prog ur calling simply

getClass() -> which is nothing but this.getClass() inside the class
thats why ur getting the output as ThreadClass...

hope u got clear

With Regards<br />Arul
Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
No I did not understand !! Please clarify further.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Referring to your second example: t is a Thread object. You created it in your constructor.

On the other hand, in the run() method, this is a reference to the Runnable object not the Thread object. That is, this is a reference to an instance of your RunnableMethodClass.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link:
subject: Threads
It's not a secret anymore!