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

Markus Green's Question

 
Saket Barve
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Dave Johnson
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

....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
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic