aspose file tools*
The moose likes Beginning Java and the fly likes is there any benefit for doing this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "is there any benefit for doing this" Watch "is there any benefit for doing this" New topic
Author

is there any benefit for doing this

sai donthneni
Ranch Hand

Joined: Mar 26, 2006
Posts: 50
public class ClassThread
{

public void print()
{
System.out.println("you are inside print() method");

}

public static void main(String args[])
{
ClassThread ct=new ClassThread();

Runnable ra=new Runnable(){
public void run()
{
ct.print();
}
};
Thread t=new Thread(ra, "ThreadA");

}

}


What is the special advntage of using local class Runnable instead of class
ClassThread impementing Runnable and overidding run() method?
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
easy: if you extend Thread and overwrite the run() method, you don't have the option to extend any other class. while this is not important for hello-world threads, in real-life applications you'll most probably want your thread (small t) to exend some sort of business-class.

hope it helped,
jan
sai donthneni
Ranch Hand

Joined: Mar 26, 2006
Posts: 50
So, is this how real world applications are written?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

What Jan said is true, but I don't think s/he looked carefully at the code. The question is not about extending Thread or using Runnable; the question is about using an anonymous Runnable class rather than making "ClassThread" implement Runnable itself.

In this little program, it makes no difference. In a real world program, you might do this if there are a number of different kinds of Runnables that need to work with ClassThread objects. If, on the other hand, you really are implementing the notion of "ClassThread running in a Thread", then the anonymous Runnable is just sillt, and ClassThread should itself implement Runnable. Anonymous classes aren't pretty, and using them for no reason just clutters your code.


[Jess in Action][AskingGoodQuestions]
sai donthneni
Ranch Hand

Joined: Mar 26, 2006
Posts: 50
Hi Ernest...So, if public void run() method has 2 or more implementations then anonymous classes should be used. Thats what you mean?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I mean that if you need two or more "run" methods, then they have to be in separate classes, and if they're very short, then an anonymous class is a convenient way to do it. There's no "should", though; if you want to use a named class, that's just fine too.
sai donthneni
Ranch Hand

Joined: Mar 26, 2006
Posts: 50
Hi Ernest, I am not sure if i am right in understanding you. When you say in two different class, you mean that two differnt classes implementing Runnable interface and overridding run() method in them?.... if it is not what ur saying then i am really very sorry and i believe i understood wrong.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is there any benefit for doing this