Win a copy of Spark in Action this week in the Open Source Projects 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

Reassigning object value within method, with class having instance of its own type

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

Consider the following question coming from one of Enthuware's mock exams.

What will the following class print when compiled and run?


With the answer being It will throw an exception when run. and the explanation being When method setIt() executes, x.link = y.link, x.link becomes null because y.link is null so a.link.value throws NullPointerException.

How is it possible that setting x.link to null affects a.link? No new value is assigned to a.link after it's initialization (a.link = b), as far as I would say. I thought reassignment of a variable within a method is not reflected in its original value (a.link). Am I wrong? I would think setIt() only affects b.link, but it seems to affect both a.link and b.link. I suspect the unusual construction (class Holder having an instance variable of its own type) has something to do with it, but I can't figure out what it is. Could it be because this method doesn't reassign the Object itself, but rather a member of the Object (namely link)? If so, how does this work?

This is one of the more complex questions of the Enthuware mock exams (in my eyes), so I'd really appreciate your explanations! Thanks again.

Regards,
Shane
 
Saloon Keeper
Posts: 12126
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:No new value is assigned to a.link after it's initialization (a.link = b), as far as I would say.


There is though. When you call setIt(a, b), the statement x.link = y.link performs the reassignment.

I thought reassignment of a variable within a method is not reflected in its original value (a.link). Am I wrong?


Reassignment of method parameters is not reflected in the variables that you passed to the method, because arguments are passed by value. So if setIt(a, b) did x = new Holder(20); a would not refer to that new Holder.

What happens here is that a's value is a reference to a Holder object. The reference is copied when you call setIt(a, b), so changing x will not change a. However, a and x do both refer to the same object, and a.link and x.link refer to the same object field. Reassigning the value of that field will be reflected in the object that a refers to.

In short: You can not change the value of a variable that you pass to a method from within that method. You CAN change fields of objects that a method parameter refers to.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shane Jensen wrote:How is it possible that setting x.link to null affects a.link? No new value is assigned to a.link after it's initialization (a.link = b), as far as I would say. I thought reassignment of a variable within a method is not reflected in its original value (a.link). Am I wrong? I would think setIt() only affects b.link, but it seems to affect both a.link and b.link. I suspect the unusual construction (class Holder having an instance variable of its own type) has something to do with it, but I can't figure out what it is. Could it be because this method doesn't reassign the Object itself, but rather a member of the Object (namely link)? If so, how does this work?


Have a look at this topic which has some excellent explanations (even with some diagrams) about the same code snippet. I am pretty sure all your doubts will be cleared after you've read that topic And if not, just let us know by hitting the "Post reply" button
 
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic