0

I have the following code

public class A {
    public class B {
    }

    public boolean wasCreatedFromMe(B obj) {
       // I want to implement this method
    }
}

public class Test {
    public static void main(String[] args) {
        A a1 = new A();
        A a2 = new A();
        B b1 = a1.new B();
        a1.wasCreatedFromMe(b1); // true
        a2.wasCreatedFromMe(b1); // false
    }
}

I would like to implement that method above which determines if the object was created from this Outer Class instance. Is there a way to use instanceof or some type of Class<> magic to do that?

I do NOT want to do any of the following:

Use data structures

// inside class A
Set<B> childObjs = new HashSet<>();

public B() {
    childObjs.add(this);
}

Ask inner class object

// inside class A
public boolean wasCreatedFromMe(B obj) {
    return obj.parent() == this;
}

class B {
    public A parent() {
        return A.this;
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Hatefiend
  • 3,416
  • 6
  • 33
  • 74
  • Reflection API can help you here. – Govinda Sakhare Aug 11 '19 at 06:21
  • @GovindaSakhare Is there no other way, ex. through `instanceof` ? – Hatefiend Aug 11 '19 at 06:29
  • Not much of a duplicate. No actual answers to this question short of reflection. – user207421 Aug 11 '19 at 06:41
  • Possible duplicate of [Getting hold of the outer class object from the inner class object](https://stackoverflow.com/questions/1816458/getting-hold-of-the-outer-class-object-from-the-inner-class-object) – Antoniossss Aug 11 '19 at 08:01
  • Here it is https://stackoverflow.com/a/15265900/1527544 – Antoniossss Aug 11 '19 at 08:01
  • @Antoniossss The only useful answers there are the reflective ones, and they don't fully answer this question. This has already been closed as a duplicate of that, and reopened. – user207421 Aug 11 '19 at 08:16
  • And that particular answer is linked. Having method of extracting enclosing class instance breaks down this question to simple `==`comparision of instances which is not even worth mentioning. – Antoniossss Aug 11 '19 at 08:19
  • 1
    It absolutely is a duplicate of the linked questions. The reason the answers there don't fulfil your conditions is that it's just not possible to fulfil them. You saying "but I wanna" doesn't change that fact, nor does it make the question less of a duplicate. – daniu Aug 11 '19 at 08:56
  • @daniu Perhaps you could suggest which of the three routes you would take then if you absolutely had to have this functionality? Would you do reflection (despite that user saying *"You could (but you shouldn't) use reflection for the job"*), sub-class method, or use a `Set`? – Hatefiend Aug 12 '19 at 02:38
  • Definitely sub-class method, ie `class Inner { boolean createdFrom(Outer o) { return Outer.this == a); }}`. You can then also call this from `Outer` by `boolean createdByMe(Inner inner) { return inner.createdFrom(this); }` which is exactly the spec you were going for. – daniu Aug 12 '19 at 07:51

0 Answers0