• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object.otherObject = yetAnotherObject

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Divyesh,

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

Matt
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, this is a good point. Thank you all very, very much.
 
reply
    Bookmark Topic Watch Topic
  • New Topic