• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object.otherObject = yetAnotherObject

 
Matt Pavlovich
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All!

I am studying for my SCJP, and am a bit stumped on something simple. I understand the dot operator for the most part, but when one object reference uses it on another, I get a bit lost. Here is the code that I am having problems with:

a1.b1 = b2 //assume all of these references are to objects and that they have already been initialized and that they take no arguments. a1 refers to Class Alpha, and b1 and b2 refer to Class Beta.

So my question is, (to the kind person willing to help me), in what real world situation would I use this? It might help me understand what is going on to have a frame of reference. And when one object calls another like this, what is really happening? I understand a1.doSomething();, or a1.size = 14; but what does it mean to use the dot operator on another object reference? What is the logic? I am very confused here.

Thanks to everyone!
Matt
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no "calling" going on here. All that happens is the the object reference in b2 gets copied to b1, which just happens to be a member of a1.
 
Matt Pavlovich
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, I see. This is from a test question in my SCJP study guide. They also have a1.b1=b1. Which would mean that b1 is copying itself? They like to make things tricky. Also, when you say, "A member of a" what exactly do you mean? If I just wanted to copy b1 to b2, I would just set them equal to each other. Is there a logical reason to add the a1.b1 side? Thanks again.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. b1 has nothing to do with a1.b1.
 
Matt Pavlovich
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so when the code says a1.b1 (and they are both object references), is a1 doing anything with b1?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The term a1.b1 indicates that whatever object is referenced by a1 has a b1 field member (aka instance variable).
 
Matt Pavlovich
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OKay I see. So b1 is just a1's instance variable. That makes sense. Is there any time, though, when I would actually do that? I am still struggling with why that would be done. In other words, to get what affect in a real program - not just a test question. I can easily think of a reason to use a1.size = 14, but a1.b1 not so much. Thanks again.
Matt
 
Divyeshh Patel
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is applicable to all places where you have object inside an object. e.g. You have a Person class and that class contains an Address.

In this case you might have to use 2 dots if you want to access, say city, directly.
p1.a1.city where p1 is Person object and a1 is Address object.
 
Matt Pavlovich
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Divyesh,

Thanks very much. That makes a whole lot more sense to me now.

Matt
 
Rodmar Conde
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although in real life situations you won't use that kind of access. You'll use setters to access instance variables.

For instance:

a1.setB1(b2); instead of a1.b1 = b2;

 
Matt Pavlovich
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this is a good point. Thank you all very, very much.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic