• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

double indirection

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
Yes, I know that subject is "C-ish", but please dont condemn me, grin. I have a class C that creates an instance of class B. Now class B has an instance variable of class A. From class C I want to get access to an member variable of class A. I am not extending the classes so they are not on the stack, but how do I get access to that "buried" variable? My trivial understanding is something like IWant = C.B.A.ThisVariable.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

of course this is the minimum to do, variables should be private and access via getter methods.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Get comfortable with making this work, then come back and ask why it might not be a good idea some times. I'm trying to tease you into getting interested in more advanced design ideas. Let us know how this goes.
 
Chris Pat
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stan
Okay, so I was crudely right. From my reading the bad thing about this is that the instance variables are public and other classes can use them "behind my back". What I should do is make them private and proxy them with s/getters. How "deep" can I go with that "." referencing?


Originally posted by Stan James:
Get comfortable with making this work, then come back and ask why it might not be a good idea some times. I'm trying to tease you into getting interested in more advanced design ideas. Let us know how this goes.

 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can go arbitrarily deep.

Any time class A needs to access a variable in a class B object it's holding, that's a sign that you've probably got a problem with your design: the code that you're writing in class A probably belongs in class B.

If you're writing code in A and you have a reference to B and B has a reference to C, and A needs a variable in C, then you don't "probably" have a flawed design: you absolutely have one. The code absolutely doesn't belong in A: it belongs either in B, or, ideally, in C.

And if you feel the need to type "A.B.C.D.x", then don't be surprised if the Java Police kick down your door and yell FREEZE!
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> From my reading the bad thing about this is that the instance variables are public and other classes can use them...

Yes, a long-standing C++ convention that is also used in Java. C++ has far better protection facilities such as declaring methods as immutable, having "protected" apply to sub-classes and not everything in the package, for example.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I first learned OO and worked with some pretty cool Smalltalk experts long chains of variables were common. I don't know when people realized that every class suddenly knew a whole lot about a bunch of other classes and that made them all fragile - change one and the whole chain breaks. It's better to just ask an object to do something.

topLevelObject.doSomethingUseful

and let that object hide how it does it. For a humorous look at this, see Don't Confuse Your Dog near the bottom of that link.
 
Remember to always leap before you look. But always take the time to smell the tiny ads:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic