lahiru nanayakkara wrote:why we cant assign thread object address to reference variable?explain
Not sure what you mean by object address as we don't really deal with addresses in Java. And what reference variable? Reference variables have a type.
You know that the following is legal..
And following is not allowed.
A Thread is just any other object. And Java allows you to assign a sub class reference/object to a super class reference without casts. Nothing to do specifically with threads there. Is that what you were asking?
lahiru nanayakkara wrote: No,I mean we cannot assign thread object to "t1" variable.why is that?
Thread t1=new Thread();
Ok, I'm guessing you mean why we 'shouldn't' ( more on the lines of why it is pointless ) do the declarations such as the one you have mentioned. This is because the default run method in the Thread class does nothing. Of course you created the thread so it does something for you.
But if a Thread is created by passing a Runnable ( a Thread is a Runnable, and a Thread subclass - although it is a bad design at least in this context - is also a Runnable ) to its constructor, and this Runnable overrides the
method thereby specifying what instructions the Thread should execute once it is started, it makes sense to create a Thread.