# Objects in objects

**URL:** <https://discourse.processing.org/t/objects-in-objects/22486>\
**Category:** Beginners\
**Created:** [July 8, 2020, 6:39pm UTC](https://discourse.processing.org/t/objects-in-objects/22486 "2020-07-08T18:39:52Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 8, 2020, 7:40pm UTC](https://discourse.processing.org/t/objects-in-objects/22486/2 "2020-07-08T19:40:23Z")

</div>

> [@carnalizer](#):
>
> Why can I say “The apple that is in this tree”, but not “The tree this apple is in”?

In general an object shouldn’t need to be aware of any other object which “owns” it.

But if you feel you need such feature you can create an “owner” field for it:

```auto
/**
 * AppleTree (v1.0.0)
 * GoToLoop (2020/Jul/08)
 * https://Discourse.Processing.org/t/objects-in-objects/22486/2
 */

import java.util.List;

Tree tree;

void setup() {
  tree = new Tree().bearFruit().bearFruit();
  println(tree);
  println(tree.apples);
  exit();
}

class Apple {
  Tree owner;

  Apple() {
  }

  Apple(final Tree tree) {
    owner = tree;
  }

  String toString() {
    return "I'm an " + getClass().getSimpleName() + " from " + owner;
  }
}

class Tree {
  final List<Apple> apples = new ArrayList<Apple>();

  Tree bearFruit() {
    apples.add(new Apple(this));
    return this;
  }

  String toString() {
    return "I'm a " + getClass().getSimpleName();
  }
}

```

---

_[View the full topic](https://discourse.processing.org/t/objects-in-objects/22486)._
