permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help Me 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 "Help Me " Watch "Help Me " New topic
Author

Help Me

samuel ebenezer
Greenhorn

Joined: Aug 03, 2007
Posts: 16




Tux object will be created and calls piggy method which inturn appends string s with "Samuel" and invokes the Thread Run method. In run am directly working on String "s". Am expecting the output as "Ebenezer 0 1 2 3"...
But I got only "Ebenezer" as the output whereas the answer is 4th option.. I was stucked here. Can any one explain me this?
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 118
samuel try using system.out.println statements to decode it urself.
i did it & found that it id because the system.out.println(s) prints the value immediately aftr starting the thread. untill this time the for loop hasn't even run once!!!
put sleep(10) in main method just before system.out.println(s) [trysystem.out.println(s+"I AM IN MAIN")]


SCJP 6 83%
Working on SCWCD/OCPJWCD
subhasish nag
Ranch Hand

Joined: Apr 25, 2008
Posts: 101
Hi Samuel,
here the method piggy(String s )is actually changing the parameter s(not the instance variable s ). So when we invoke the method it is only the copy of s and the change is only made inside the method, not on instace variable.
you can put System.out.println(s) just after the line: s=s+1 inside the method and can check.

subhasish


Thanks,<br />Subhasish
samuel ebenezer
Greenhorn

Joined: Aug 03, 2007
Posts: 16
Thanks Milan.. I got this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help Me
 
Similar Threads
Threads
start() method
Question based on pass by reference
a loop in a thread?
Thread question