This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes start() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "start() method" Watch "start() method" New topic
Author

start() method

Sharanya Sharma
Ranch Hand

Joined: Jul 26, 2004
Posts: 54
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

Joined: Aug 07, 2004
Posts: 15
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().


SCJP 1.4 (95%)
 
jQuery in Action, 2nd edition
 
subject: start() method
 
Similar Threads
urgent!!!! Marcus Test 1 Threads
Thread question from Marcus green - Exam1
output of the thread
Marcus Green Mock- doubt
Thread - Please explain!