Thread t = new Thread(this); t.start(); (when used in static context we get a compile time error) (when used in a method it runs fine) but i read some where that Thread constructor has no constructor like Thread(this) is it true if so why is the method runnig fine.
Hi: I guess the reason for compilation error in Static context is that, The static methods don't have "this" reference. Since the static members of a class are not associated with any object instead they are of class only. hence, they can't have "this" reference.
Whereas when u call it in a non static method, u can easily pass "this" to the threads constuctor. As that methods must have "this" reference.
About the threads Constructor, I can't say much until I see the code...If "this" is refering to a runnable target then u can surely pass it in threads constructor...
I suppose you are writing the code somewhat like this.
i.e. the class that you have declared is extending the "Thread" class or it is implementing the Runnable interface. In that case it is perfectly legal to write this Thread newT= new Thread(this); beacuse the "this" in the above code refers to your class and in the "Thread" class we have a constructor that takes a Thread argument or Runnable target.
sorry i misread it says that there is no this variable in static main method to pass to the thread constructor.any way discussing with u cleared my doubt and also understand it clearly.thanks
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop