This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Getting the object that created this object 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 » Java » Java in General
Bookmark "Getting the object that created this object" Watch "Getting the object that created this object" New topic
Author

Getting the object that created this object

Adam Cripps
Ranch Hand

Joined: Oct 01, 2011
Posts: 41

I have a object which needs to change a variable of an object that created it - is there any way for the object to find out which object created it (and hence give me a handle on the variable)?

Thanks.
Adam
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Only if the object keeps a reference to its creator.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Adam Cripps wrote:I have a object which needs to change a variable of an object that created it -


That sounds fishy. Why would an object care which object created it? And what if no object created it? Or what if the object that created it doesn't have that variable?

Can you explain why you want to do this?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65



[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

Hi Adam

It sounds like you need to redesign your model here. Can you provide more information on your code base please?
Adam Cripps
Ranch Hand

Joined: Oct 01, 2011
Posts: 41

I have a JFrame which holds an JTextArea. Also, inside the JFrame is a TabbedPane - I created new instances of the TabbedPane so that I could handle new ones in the future if they come up.

The TabbedPanes each have JTextFields, which I want to getText from and then insert that in to the JTextArea.

I have fixed this - when I created an instance of TabbedPane, I passed the JFrame instance and then created a class variable instance of this so that actionPerformed could get a hold of it.

You can now mark this as a non-issue.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37913
    
  22
Why didn’t you use the methods to get a parent or ancestor object?
Adam Cripps
Ranch Hand

Joined: Oct 01, 2011
Posts: 41

@CampbellRitchie - I didn't use them because I don't know about them - can you give me a pointer? Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37913
    
  22
No.


Oh, all right then, click the no and you should find something useful. Then scroll up to the method summary and you can find other methods.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37913
    
  22
Bear Bibeault wrote:
I though that applied to bulls.
Adam Cripps
Ranch Hand

Joined: Oct 01, 2011
Posts: 41

Thanks for the link - I did see this link but was confused as to whether it would return the object that instantiated the class that calls the getParent, or whether it returned the super class. I will investigate.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Adam Cripps wrote:Thanks for the link - I did see this link but was confused as to whether it would return the object that instantiated the class that calls the getParent, or whether it returned the super class. I will investigate.


It doesn't return either of those things. It returns the Swing component which contains the component which calls the method. For example if you add a JLabel to a JPanel, then calling the JLabel's getParent() method returns a reference to that JPanel. That's what "parent" means in the Swing component hierarchy.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Paul Clapham wrote: It returns the Swing component which contains the component which calls the method. For example if you add a JLabel to a JPanel, then calling the JLabel's getParent() method returns a reference to that JPanel. That's what "parent" means in the Swing component hierarchy.


In particular, in case it's not clear, "parent/child" in Swing's component hierarchy has nothing to do with "parent/child" in a class relationship. In the context of Swing components being discussed here, it's about which GUI element manages or owns or contains another for things like events and window management, and they will in general not be in a parent/child class relationship.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Campbell Ritchie wrote:
Bear Bibeault wrote:
I though that applied to bulls.

And to code smells.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37913
    
  22
You can get at methods from the superclass with the super keyword.

You should not call superclasses parents. A parent-child relationship is a GUI term as Jeff Verdegan has told you. It means the parent object surrounds the child object on the display.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting the object that created this object
 
Similar Threads
Reference Store
which memory is used when instance variable of an object is instantiated?
understanding an object...
catch (Exception parameter)
explanation of Interface concept