This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing this to a constructor

 
Jasper Vader
Ranch Hand
Posts: 284
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
}
}
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 284
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 366
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 284
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 284
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic