wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object.otherObject = yetAnotherObject Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object.otherObject = yetAnotherObject" Watch "Object.otherObject = yetAnotherObject" New topic
Author

Object.otherObject = yetAnotherObject

Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
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

Joined: Jan 10, 2002
Posts: 60752
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
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

Joined: Jan 10, 2002
Posts: 60752
    
  65

No. b1 has nothing to do with a1.b1.
Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
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

Joined: Jan 10, 2002
Posts: 60752
    
  65

The term a1.b1 indicates that whatever object is referenced by a1 has a b1 field member (aka instance variable).
Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
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

Joined: Aug 03, 2010
Posts: 37
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.


Cheers,
Divyesh.
Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
Divyesh,

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

Matt
Rodmar Conde
Greenhorn

Joined: Jul 09, 2010
Posts: 11
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;



SCJA | SCJP Semi Study Guide in Spanish
Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
Yes, this is a good point. Thank you all very, very much.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Object.otherObject = yetAnotherObject
 
Similar Threads
Self Test Answers Chapter 3 Question 11
Double K&B Chapter 3- Question 11
Garbage Collection Clarification
Garbage Collection problem-2
What does this assignment mean?