user3818619
user3818619

Reputation:

Adress a member of Object inside such a member

I tried to simplify as much as possible to not spam you with a wall of code from my game

Say I have a Object of the class XY which holds a ArrayList of the Objects Z. How can I address a member (int number) of the XY Object from a methode inside the class Z.

class XY {

   static int nextID = 0;

   final int id;

   int number;  

   List<Z> zList = new ArrayList<Z>();

   XY(int number) {
      this.id = nextID++;
      this.number = number;
      zList.add(new Z(id));
}

..

class Z {

   final int id;

   Z (int id) {
      this.id = id;
   }

   void getXYNumber() {
      int n = // ?
   }

}

I could only find a workaround because in my case the class XY is also just stored as an Array in the class calling it now ABC and all Z objects have the same id as the class XY which holds the Z objects, so I can use this id to identify the object XY inside ABC from the methode getXYid inside Z, I'm calling.

class ABC {

   static XY[] = new XY[]{12, 45, 86};

   static XY getXY(int id) {
      for(XY xy: xyList) {
         if (xy.id = id) return xy;
      }
   }

}

class Z {

   void getXYid() {
      int n = ABC.getXY().number;
   }

}

Is there any keyword or something to get the specific XY, which hold the Z with the methode I'm calling, or this already the best solution?

Note: In this case the member to identify the class is the int id which could just be used as a index from the XY Array inside the ABC class, but in my case the object to identify the class is a enum called Owner that are all the same for all Z objects inside the XY object, so that wouldn't work out.

Upvotes: 1

Views: 81

Answers (2)

Robby Cornelissen
Robby Cornelissen

Reputation: 97381

Create a field of type XY in Z, and set it's value when creating Z. It would also remove the need for copying the id field.

class XY {
   static int nextID = 0;

   int id;
   int number;  

   List<Z> zList = new ArrayList<Z>();

   XY(int number) {
      this.id = nextID++;
      this.number = number;
      zList.add(new Z(this));
}

class Z {
   XY xy;

   Z (XY xy) {
      this.xy = xy;
   }

   int getXYId() {
       return xy.id;
   }

   int getXYNumber() {
      return xy.number;
   }
}

This type of circular reference is generally considered bad practice and in many cases indicates that you need to review your application architecture/design.

Upvotes: 1

Simon
Simon

Reputation: 1416

I believe there are two simple solutions. If the value of number in XY does not change, I would suggest passing number to the constructor of Z like:

class Z {

   int xyNumber
   final int id;

   Z (int id, int xyNumber) {
      this.id = id;
      this.xyNumber = xyNumber;
   }

   void getXYNumber() {
      int i = // ?
   }

}

In the above example, you can call your constructor like: new Z(id, number);

If the value of number will change, you can pass the actual XY object to the constructor of Z. This object is a reference so if the original changes, the version inside the Z object also changes. This gives you the most current number value. When the Z object is passed to another XY, you can update the currentXY using the void setXY(XY)

class Z {

   final int id;
   XY currentXY;

   Z (int id, XY currentXY) {
      this.id = id;
      this.currentXY = currentXY;
   }

   void getXYNumber() {
      int i = // ?
   }

   void setXY(newXY) {
      currentXY = newXY;
   }

}

In the above example, you can call your constructor like: new Z(id, this);. they keyword this will give you the current object you are working with, which in your case is the XY object.

Upvotes: 1

Related Questions