Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help Me

 
samuel ebenezer
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




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
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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")]
 
subhasish nag
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
samuel ebenezer
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Milan.. I got this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic