Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Objects and reference

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I'm having some problems understanding what's going on in this class (part of an exercise on) Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 25122-25127). Enthuware. Kindle Edition.


And basically this throws NullPointerException when run.
I've been trying to "visualize" this class, but I don't seem to be able to understand the mechanics of it. So, for example:

in line 8 we create a final object of type Holder, which we know can't have its reference changed to anything else. So this object can be visualized like this:
a   ----value = 5
    -----link = null
Then we create another Holder object, b, so that
b   ----value = 10
     ----link = null
Link in both cases isn't initialized so it's null
What happens in line 10 when we say We're essentially saying that a.null points to b, but how can a.link point to b and not to b.link? Sorry this class is reallly confusing me.



 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean, to elaborate a bit more, what I find confusing is the assignment : we're assigning an object (b) to a.link and with that assignment a.link is 10 (I tried the code and I'm playing with it right now). What I don't get is that b is an objet so how do I get the value 10 out of that object and copy it into a.link? That's what - I suppose - I'm not understanding right now
 
Henry Wong
author
Marshal
Pie
Posts: 22119
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:I mean, to elaborate a bit more, what I find confusing is the assignment : we're assigning an object (b) to a.link and with that assignment a.link is 10 (I tried the code and I'm playing with it right now). What I don't get is that b is an objet so how do I get the value 10 out of that object and copy it into a.link? That's what - I suppose - I'm not understanding right now


As you have already mentioned, there are four Holder reference variables. Two are the local variables a & b. And the other two are the link instance variables of the two Holder objects that have been created. As for the two Holder objects, one created at line 8 and one created at line 9, well, I am going to call one object8 and the other one object9, based on the line number that they are created. This is purely for explanations purposes, as there are no variables with those names.

Anyway...Remember that the left side of the assignment is used specify the variable to be assigned, while the right side of the assignment is used to evaluate to the value that will be assigned.

So, in this case, "a.link" works out to the link instance variable for object8. And "b" works out to the object9. So, this means that the holder instance variable of object8, will be set to point to object9.

No new objects are created. No objects are copied. We are simply setting a reference variable to point to an object.

Henry



 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, thanks for that.
So a few questions. What would be the difference between doing ; and ?
Let me have a go please! The first one will assign the value 10 to a.link, or, to be more precise, a.link will point to b and then gets 10, the second one we get a.link to point to b.link (which is null) and therefore a.link will also get null.

I get all you said below, but, what I can't get past in my head is the fact that b is an object and therefore contains its own copy of value and link, so when a.link is set to point to object b, how does it get the value of 10 from b if b contains not only value but a link variable? Meaning what it the mechanism that says "Oh OK so when  a.link = b; let me go and find the value which sits inside b.value and copy it into a.link"?

Moving on into the execution of the program, here is what happens (let's see if I get things right):
inside x.link's value is 10 but y.link is still null because it hasn't been assigned any reference, so the assignment effectively assigns null to x.link (a.link), but the method is returning x which is a reference to "a" and "a" is not null.
So when we run we effectively say
Correct?
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:so when a.link is set to point to object b, how does it get the value of 10 from b if b contains not only value but a link variable? Meaning what it the mechanism that says "Oh OK so when  a.link = b; let me go and find the value which sits inside b.value and copy it into a.link"?


It doesn't have to find and copy value of b.value to a.link. Here a.link is an instance variable of type Holder of an object referred by a, so It just assigns the reference of an object referred by reference variable b of type Holder to a.link. So now both a.link and b refers to the same object having instance variable value = 10 and link = null.

we effectively say

Correct?

About first a.link.value means null.value is correct but second part b.link.value is not null.value because method setIt(..) returned x(Or we can say a) which is assigned to b.link so now a and b.link referes to the same object having instance variable value = 5 and link = null; so If you just remove a.link.value from print statement and print only b.link.value It will print 5.

Nitpicking, It doesn't compile you forgot to close class properly. I mean need one more } 
 
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
Here you'll find another topic on the same question.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic