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.
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.
The term a1.b1 indicates that whatever object is referenced by a1 has a b1 field member (aka instance variable).
Joined: Aug 14, 2010
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.