• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

start() method

 
Sharanya Sharma
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please look at this code
public class Tux extends Thread{
static String sName = "vandeleur";
public static void main(String argv[]){
Tux t = new Tux();
t.piggy(sName);
System.out.println(sName);
}
public void piggy(String sName){
sName = sName + " wiggy";
start();
}
public void run(){
for(int i=0;i < 4; i++){
sName = sName + " " + i;
}
}
}


The answer to the above question is given as
Compilation and probably output of "vandelur" but possible output
of "vandeleur 0 1 2 3"

But the start method should be called on an instance. Please let me know if Iam wrong.
 
Aarti Dahiya
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A reference to the object which invokes the non-static method is passed IMPLICITLY to the non-static method.
The method call t.piggy(sName) invokes the method piggy. Apart from the reference t, piggy has its own reference to the object referenced by t. It has been passed implicitly. So, when start() is called it is actually t.start().
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic