permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Passing this to a constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Passing this to a constructor" Watch "Passing this to a constructor" New topic
Author

Passing this to a constructor

Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
A compile time error is generated in the following code, because of the way that the TSamp constructor is called. I assume there is just a default constructor provided for TSamp. But when the TSamp constructor is called from the method of go() from the the Multi object, a parameter of (this) is passed to the constructor, which means that a reference to the current object m is being passed to the constructor (and there is no apropriate constructor to accept such a parameter) ... is that why the compile-time error occurs?
class TSamp extends Thread{
public native String getTime();

}
public class Multi implements Runnable {
boolean bStop;
public static void main(String argv[]){
Multi m = new Multi();
m.go();
}
public void go(){
TSamp ts = new TSamp(this); // the call
// to the TSamp constructor
ts.start();
bStop=true;

}
public void run(){
if(bStop==true){
return;
}
System.out.println("running");
}
}


giddee up
Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
Jasper,
thats right.
try including this in TSamp class
public TSamp(Runnable r) {
super(r);
}
And everything works fine
Hope this helps
Sri
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
Sri, Brilliant, thankyou! so that places a constructor ready to take an object of type Runnable (and seeing that an object of Multi is passed to it, everything is okay, because an object of a class that implements an interface can be cast to that interface)...
but why, in the TSamp constructor, put the call to the superclass constructor of the TSamp?
Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
Jasper,
super(r) in the code is placed to call the thread constructor which takes Runnable target.
Thread (Runnable target) where target is the reference to the Runnable Interface or a class that implements Runnable interface.
Hope this helps
Sri
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
From the API, this particular constructor...
Allocates a new Thread object. This constructor has the same effect as Thread(null, target, gname), where gname is a newly generated name. Automatically generated names are of the form "Thread-"+n, where n is an integer.
Parameters:
target - the object whose run method is called.
...interesting.
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
Originally posted by Sri Sri:

super(r) in the code is placed to call the thread constructor which takes Runnable target.

aha, i see...and constructors are not inherited, hence the call.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing this to a constructor
 
Similar Threads
thread doubt
Threads
A question on threads
doubt on thread
doubt on thread