• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

how does an object talk to one that instantiated it?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is probably a dumb question, but I can't
seem to get it...
How does an object talk to the object that instantiated it?
I have a three class program. The class 'display' that contains
the 'main' method instantiates an object 'mg' of
class 'MyGui', and an object 'db' of class 'Database'.
I want the listeners in mg to be able to tell display
when I have pushed a button, edited a text field, etc..
But I stupidly can't seem to figure out what the reference
to display is, from mg's viewpoint. The only way I have
had mg transmitting information back to display thus far
is via the return values from mg's methods, when display
calls them.
I'd also like to get mg and db to communicate, without
doing set/gets up to display, and them down to the
other object.
This has got to be obvious....
 
Bill Vannah
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should probably re-word this a bit.
The question is, how does an object access
fields in the object that instantiated it?
For example, if object 'mg' is instantiated within
a 'main' method, how might mg later learn the
values/addresses of various fields within main?
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bill Vannah:
I should probably re-word this a bit.
The question is, how does an object access
fields in the object that instantiated it?
For example, if object 'mg' is instantiated within
a 'main' method, how might mg later learn the
values/addresses of various fields within main?


Except in the case of Inner Classes, All Objects are seperate and can only access the methods and variables that the other class defines with an access modifier that makes them reachable by the new Object. Instance methods and variables require that the new class have an instance of the creating class either passed to it on creation or it creates one of it's own. Static methods and variables are available without an instance.
ie.
Class A creates an Instance of Class B
If A and B are in the same package B can access methods and variables that are public, private and default.
If A and B are not in the same package and B is a subclass of A then B can access methods and varabiles that are public and private.
Otherwise, B can only access public items.
I hope this helps
 
Bill Vannah
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class A creates an Instance of Class B
If A and B are in the same package B can access methods and variables that are public, private and default.
If A and B are not in the same package and B is a subclass of A then B can access methods and varabiles that are public and private.
Otherwise, B can only access public items.
I hope this helps
Thanks for the reply, Carl.
The question I have is what B should call
variables in A in order to access their value/reference.
For instance, class A has main in it, and creates an instance B of some class 'OtherClass'. Everything is in the same package. Class A also has a primitive variable in it called 'z'. What would B call it in order to learn it's value? B won't call it 'z, because B could have its own variable called 'z'. 'A.z' and 'main.z' don't fly either.
What I really have is class A, which instantiates mg of class 'MyGui' and db of class 'database', and I'd like mg
& db to be able to communicate directly via get/set calls,
without going through A. I know about the permission stuff,
I just don't know how to refer to the variable. For instance,
if db has a method 'getDensity', how do I call that from
mg. I don't think its just 'getDensity', or 'db.getDensity',
but seems like I ought to be able to do this, right?
[/B]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's possible I'm misunderstanding your problem. But. From what I vaguely gathered from your problem, this is what you need to do.

Your best bet is to pass in the object you want via a constructor or a secondary method, something contained in class B (for instance).

..and then in class A, call the setA method: B.setA(this);
 
Bill Vannah
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


AHHH..ok, Many thanks, Yuhri (and Carl). I needed
to pass the references around right. Overall, I
still need more practice thinking in OO terms.
The objects of class Gui and Database can now talk
to each other.
By the way, all the examples I saw when looking this up
were of the type "object Top which instantiates object
myGui which instantiates object myDatabase", in in which
Top would refer to myDatabase as myGui.myDatabase. The structure I was working with was "object Top which instantiates
object myGui and object myDatabase". In the first case,
the instantiations are in a straight path, in the second there
is a fork. All the examples I could find when I looked
through the books and code were 'straight path' type -
including, by the way, the View-Doc architecture
that MFC (Visual C++) sets up for you, where View
instantiates an object of Doc. So, am I trying to
do something back-asswards here? This is mainly a rhetorical
question, you guys have helped enough! But if anyone
on the list is interested in a discussion of this,
I am sure curious how it 'ought' to be done.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic