File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help Me " Watch "Help Me " New topic

Help Me

samuel ebenezer

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: 122
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%
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.


Thanks,<br />Subhasish
samuel ebenezer

Joined: Aug 03, 2007
Posts: 16
Thanks Milan.. I got this.
I agree. Here's the link:
subject: Help Me
It's not a secret anymore!