wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about thread" Watch "about thread" New topic
Author

about thread

wei liu
Ranch Hand

Joined: Dec 06, 2001
Posts: 56
Question 19)
What will happend when you attempt to compile and run the following 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;

}
}
}
1) Compile time error
2) Compilation and output of "vandeleur wiggy"
3) Compilation and output of "vandeleur wiggy 0 1 2 3"
4) Compilation and probably output of "vandelur" but possible output of "vandeleur 0 1 2 3"
ans: 4) Compilation and probably output of "vandelur" but possible output of "vandeleur 0 1 2 3"
my question is that why there are two possibilities?
Tony Sam
Ranch Hand

Joined: Nov 19, 2001
Posts: 32
becoz method run() may possible exexcute after
System.out.println(sName);


Tony Sam<BR>One want to be a SCJP...
wei liu
Ranch Hand

Joined: Dec 06, 2001
Posts: 56
I thought the ans was c ,since the code will first run t.piggy(sName);secondly System.out.println(sName);in that case the result should be c.
what is the wrong point of my view?

Originally posted by wei liu:
Question 19)
What will happend when you attempt to compile and run the following 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;

}
}
}
1) Compile time error
2) Compilation and output of "vandeleur wiggy"
3) Compilation and output of "vandeleur wiggy 0 1 2 3"
4) Compilation and probably output of "vandelur" but possible output of "vandeleur 0 1 2 3"
ans: 4) Compilation and probably output of "vandelur" but possible output of "vandeleur 0 1 2 3"
my question is that why there are two possibilities?

prawith thowphant
Greenhorn

Joined: Sep 20, 2001
Posts: 5
Hi friend.
look at piggy mehtod.
at line sName = sName + " wiggy";
now sName is not reference to sName that data member of class.
because, String it cannot chage the value.
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
Your view is right if

SomeObject.sName = sName + " wiggy";
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about thread