• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OCA, page 196, minor confusion (K&B7)

 
Mark Kevin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In the middle of the page, the last sentence which reads "... it can't make f refer to a different object!", is this referring to the last statement in the above code - g = new Foo(); or something else?

In that last sentence, when they refer to f, do they mean the f that was passed into the doStuff(Foo g) method, which is now g? In other words could it also be stated another way, such as, "but it can't make f, which is now g in the doStuff() method, refer to a different object." Does that make sense? When they say f, do they really mean g? Or the f that is now g? Or in g? Or assigned to g? Or maybe they mean the original f by assigning a new Foo object to g? I don't know ...

Thanks for any help

m..
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It just means that g is a local variable. If it was called f there it would still be local, btw. The g.setName() with do its thing on f like you would expect. But g = new foo() only reassigns the local variable and doesn't affect f.

The best analogy is think of f as a remote control for a TV. When you pass it to a function, the function gets a copy of the remote. The copy of the remote can operate on the object just like the original could. But the remote can't be reassigned to operate on a different TV.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Kevin wrote:In that last sentence, when they refer to f, do they mean the f that was passed into the doStuff(Foo g) method, which is now g? In other words could it also be stated another way, such as, "but it can't make f, which is now g in the doStuff() method, refer to a different object." Does that make sense? When they say f, do they really mean g? Or the f that is now g? Or in g? Or assigned to g? Or maybe they mean the original f by assigning a new Foo object to g? I don't know ...


First let's add some context and share the code snippet to which the unclear statement belongsI can understand that the explanation in the study guide could be a little confusing, but the explanation is spot-on. So let's try to make it easier to understand.

Here we go! In the bar() method a new Foo instance is created. And a reference to this new instance is assigned to f. Meaning f's value is not the actual object, but a reference to this object. It could be the address of this object on the heap. Let's assume, the new Foo instance was created on address 1979, then the value of f is 1979. Then f is passed to the doStuff() method.
When you pass f to the doStuff() method, a copy of the value of f is passed (and assigned) to g. So g has also 1979 as value, meaning f and g have the same value and refer to the same object. So if you invoke a method on g (e.g. setName()), you invoke a method on the same object f is referring to as well (so the name of this object will be changed to "Boo"). Finally you create another Foo instance (on address 2015) and assign a reference to g. So the value of g has changed and is now 2015. But the value of f will still be 1979, because that didn't change.

But it's so much easier with an example. Let's add a Foo class with a setName() method and a TestFoo classWhen you run this code, you'll get the following output:
before doStuff: Foo1
before setName: Foo1
after setName (1): Boo1
after new: Foo2
after setName (2): Coo2
after doStuff: Boo1

So the name of the object f was referring to, is changed (by calling setName() on variable g), but although a new Foo instance was assigned to g, f is still referring to the original object.

This is a very important concept in Java. So a good solid understanding is required to pass the exam. So you'll probably want to read this excellent article as well. And if you would still have doubts or questions, don't be shy and just hit the reply button

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:The best analogy is think of f as a remote control for a TV. When you pass it to a function, the function gets a copy of the remote. The copy of the remote can operate on the object just like the original could. But the remote can't be reassigned to operate on a different TV.

I like this analogy, but it's not completely true. From the above code you can see that the copy of the remote control (g) can be reassigned to a new TV (a Foo object), but this doesn't affect the original remote control (f);

Without the function the code would look like thisAfter line2 is executed, you'll have 2 remote controls for the Foo instance created on line1. So you can use either of these reference variables to refer to the object and access a member (e.g. invoke a method). That's happening on line3: the setName() method is invoked on variable g to change the name of the object. On line4 the control of a new Foo instance is assigned to variable g but that doesn't have any affect on variable f, variable f is still refering to the same object as on line1.

Hope it helps!
Kind regards,
Roel
 
Mark Kevin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all. Now it makes more sense. The book is a bit convoluted on this topic.

Onward.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
I like this analogy, but it's not completely true. From the above code you can see that the copy of the remote control (g) can be reassigned to a new TV (a Foo object), but this doesn't affect the original remote control (f);

Yeah, I wasn't careful enough with the wording. Either f or g can be reassigned, but if one is it doesn't affect the other. The remote analogy itself is true through. When you reassign, you've reprogrammed the remote. You now control a different TV but that didn't affect the remote that yours is a copy of...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic