Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Passing by value...

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Please help me solve this confusion.....

This is from the K & B book , about passing the objects and variables to the methods.


And if you're passing an object reference variable, you're passing a copy of the bits representing the reference to an object. The called method then gets its own copy of the reference variable, to do with it what it likes.[ this is clear to me ]

But because two identical reference variables refer to the exact same object, if the called method modifies the object (by invoking setter methods, for example), the caller will see that the object the caller's original variable refers to has also been changed. In the next section, we'll look at how the picture changes when we're talking about primitives.


The bottom line on pass-by-value: the called method can't change the caller's variable, although for object reference variables, the called method can change the object the variable referred to. What's the difference between changing the variable and changing the object?

For object references, it means the called method can't reassign the caller's original reference variable and make it refer to a different object, or null. For example, in the following code fragment,
[ this line is confusing me, first they said that the variable can change for the object but not for the primitives , but in the last sentence they say that the object references also cannot change].




Can anyone please simplify this in a lucid language , may be a day to day example , so thta it becomes easier to absorb??

Thanks,
Piya
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

the caller will see that the object that the caller's original variable refers to has also been changed. In the next section, we'll look at how the picture changes when we're talking about primitives.



You meant to say the object referred to has been changed not the reference.


After the call to doStuff()
______________________

Foo f ------------------> new Foo() f: points to the object
doStuff(Foo g--------------------^) g: points to the same object



Before doStuff() completes
_________________________

Foo f ------------------> new Foo() f: points to the object
doStuff(Foo g.setName()--------^ g: performs setName() on this object
doStuff(Foo g----------> new Foo() g: points to new object after instantiation
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More over , after control goes out of 'doStuff' method , 'g' reference dies out and object to which 'g' was pointing to , becomes candidate of GC.


 
Ryan Beckett
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a good resource provided by the ranch on pass by value
 
We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic