aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Markus Green's  Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Markus Green Watch "Markus Green New topic
Author

Markus Green's Question

Saket Barve
Ranch Hand

Joined: Dec 19, 2002
Posts: 229
The question pertains to threads. However, it involves some concept of static modifier and possibly that of strings too.
Here is the code:

Now, I would have thought that once the method piggy() is called the value of static field sName is changed to vandeleur wiggy.
However, Markus says in his explanation:

Just because sName is static does not mean that passing it to a method gives the method the original copy. The method only sees a locally created copy and any changes to it will not be reflected on return to the calling method.

There is no local copy created in the method piggy since we are continuously refering to the one static instance field. This is not quite my understanding of functionality of keyword static.
Please help me clarify this doubt.
Thanks,
Saket
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Saket
Consider this : What if the method declaration of piggy had been somewhat like this public void piggy(String notasName) then it would make you think that both are reffering to different object. Well this variable is being created in a method and has method scope and will shawdow any class var. So actually what's really happening is that naming sName only makes the class's sName variable to be invisble in the piggy() method. If you wish to use the sName class var. use it in conjunction with this. eg. this.sName. Secondly as in this case the variable is also a static memeber of the class the var can also be referenced with the class name as well.
There is no local copy created in the method piggy since we are continuously refering to the one static instance field.

The copy in the piggy method is a local copy.
Hoping that helped. Well did that.
[ May 26, 2003: Message edited by: Anupam Sinha ]
Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
For this question the output is indeterminant as it could be one of the following:
vandeleur
vandeleur 0
vandeleur 0 1
vandeleur 0 1 2
vandeleur 0 1 2 3
......but "wiggy" will definitely not appear in the output. Here the tricky part is not the word static, it is that the local variable of method piggy has the same name sName as the class variable, the method local variable hides the class variable in this case.
And in method run, the value of sName does change, and with thread, you know that little is guranteed..........
you may try add a sleep in the main method to see the effect:

[ May 26, 2003: Message edited by: Yi Meng ]

Meng Yi
Dave Johnson
Ranch Hand

Joined: May 25, 2003
Posts: 111
I understand the initial question & follow it's logic. However playing around with the code a little more ie:

When the code runs the output is vandeleur wiggy.
What I can't grasp is that strings are immutable, but inside the method it appears that the +operator actually concatenates the original string sName.
Probably for the same reason as given before.
the local variable of method piggy has the same name sName as the class variable, the method local variable hides the class variable in this case.

If anyone could explain this I would be grateful, thanks, Dave.
Saket Barve
Ranch Hand

Joined: Dec 19, 2002
Posts: 229

....strings are immutable, but inside the method it appears that the + operator actually concatenates the original string sName.

Yes, the plus operator does so using the StringBuffer class and its append method. However, the result is not the same object you were dealing with before. A new String object (in this case "vandeleur wiggy") is created with the concatenation. The result is then stored in sName.
Saket
Dave Johnson
Ranch Hand

Joined: May 25, 2003
Posts: 111
Thanks for that Saket Barve, I find the area of strings being immutable and yet in this mutable confusing. Does anyone know of any links where this is explained in detail.
Where Saket Barve says:
the plus operator does so using the StringBuffer class and its append method.

This is where I get lost as no StringBuffer object exists. I know that I'm way off beam, so if someone can explain, that would be great.
Thanks, Dave.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Dave
In this code there is no StringBuffer object. The String sName is an immutable object. What's actually happening is like say i=i+10; So now in this line of code the intial value of i is incremented by 10 and then the result is stored in the variable i. Similarly in sName = sName + " wiggy"; wiggy is added to the string var. sName and this results in a new String object that cntains "vandeleur wiggy". Then sName is made to point to the new String. String's are immutable and StringBuffer's are mutable.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Markus Green's Question
 
Similar Threads
Thread question
Marcus Green's exam #1 q19 - Threads
Passing Values
GREENS EXAM 1: Q 19
urgent!!!! Marcus Test 1 Threads