• Post Reply Bookmark Topic Watch Topic
  • New Topic

Not understanding why I am getting 99 as output  RSS feed

 
kiruthigha rajan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a doubt in the following program



output :

f.myBar.barNum is 28
myBar.barNum in changeit is 99
myBar.barNum in changeit is 420
myBar.barNum after changeit is 99

i am not getting why the out put of the last myBar.barNum is 99 .please explain.thanks in advance
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags next time. It makes it easier to read your code.

You call the method passing a reference to the original Bar object. When you change the contents of this object, the original object is changed. So after line 12, f.myBar.myNum == 99 because you just changed it.
On line 15 you create a new Bar object and assign it to the method parameter. Because Java uses call by value only, this change is not applied to the f.myBar; it stays the same. The value 420 is therefore only set for this object and not for f.myBar. After the method ends, this new object is discarded and eligible for garbage collection.

In short:
- it's not 28 because you modified the object through a copied reference to the same object.
- it's not 420 because changing the value of the method-local reference does not propagate back outside the method.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Please UseCodeTags next time. It makes it easier to read your code. . . .
He did use code tags, but wrote them by hand and had an error in them, which I have corrected.

Agree, this is one of those programs about pass-by-value with design peculiarities (eg non-private fields, passing output parameters) which make them very confusing.
 
kiruthigha rajan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Rob Spoor wrote:Please UseCodeTags next time. It makes it easier to read your code. . . .
He did use code tags, but wrote them by hand and had an error in them, which I have corrected.

Agree, this is one of those programs about pass-by-value with design peculiarities (eg non-private fields, passing output parameters) which make them very confusing.

Excuse me Ritchie..im a girl not a boy
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that
 
kiruthigha rajan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Sorry about that

not an issue
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you pass the Bar object to the method changIt, you passed the copy of reference to this object. So if you make any changes to this object reference (that is pointing to actual object) you are actully modifing the actual value(this changes will persist). But when you created a new Bar object and store its refrence to myBar at line 15 you lost the reference to the actully object that you passed from main method.

So whatever changes now you make to this myBar (which is the local object to the method) those will not going to impact the object created in main method and as soon as scope of changeIt method exhaust, local object will be lost.

when you come back to the main method you are again having you original object refering to the same object that you passed to the method changeIt().

hope this helps.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!