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?
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")]
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.