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;
}
}