The Artima Developer Community
Sponsored Link

Java Answers Forum
Is it possible to get the reference of one object from its element?

3 replies on 1 page. Most recent reply: Aug 14, 2003 6:26 AM by David

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Dor

Posts: 13
Nickname: eis
Registered: Jul, 2003

Is it possible to get the reference of one object from its element? Posted: Aug 14, 2003 2:00 AM
Reply to this message Reply
Advertisement
Hello!

I am trying to implement some hierachy, say, one cell contains an array of attributes. 'cell' and 'attribute' are both classes.

The problem is I want to be able to access the other party from both sides, i.e., access 'attribute' from 'cell' (which is straightforward) and the other way around (which I don't know whether it's possible).

The 1st question is, do I need to implement something like 'pointer' in the lower class (in this case, 'attribute') to the class in which (an array of)it is contained, or is there any backward infrastructure already existing in Java that I can make use of?

The 2nd question is, if I shall have to implement this 'pointer'-like-thing, is there some type other than reference that I could use? Because if this element should be one reference, it looks like the 'attribute' also contains one 'cell', which is not at all logical.

I just began programming with Java. Please give some hints! Thanks a lot!


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Is it possible to get the reference of one object from its element? Posted: Aug 14, 2003 3:34 AM
Reply to this message Reply
One solution would be to pass the Cell to the Attibute as an attribute of a method such as Attribute.setCell(Cell c). Given that only a pointer is passed then the overhead shouldn't be too high. There may be problems though, for instance, if a single Attribute were shared by several Cells.

Having done that though, you now have two classes that are very closely coupled. You may benefit from looking at refactoring the classes and extracting the common functionality into another class.

Vince.

Dor

Posts: 13
Nickname: eis
Registered: Jul, 2003

Re: Is it possible to get the reference of one object from its element? Posted: Aug 14, 2003 5:59 AM
Reply to this message Reply
Thanks, Vince.

I don't like the idea of two things coupled together like this either, but that's some structure I can't change.

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: Is it possible to get the reference of one object from its element? Posted: Aug 14, 2003 6:26 AM
Reply to this message Reply
It might be an idea:
  public class Attribute {
    private Cell parent;
 
    public Attribute(Cell parent) {
      this.parent = parent;
    }
  }
 
  public class Cell {
    private List attributes;
 
    public Cell() {
      attributes = new ArrayList();
    }
 
    public void addAttribute() {
      Attribute toAdd = new Attribute(this);
      attributes.add(toAdd);
    }
  }

I'm guessing at the structure, but you get the point: pass 'this' into the constructor.

Flat View: This topic has 3 replies on 1 page
Topic: ClassLoader help Previous Topic   Next Topic Topic: Ensuring single instance of app?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use