is possible to know in a class who has instanced itself?
I have a class Listener that can be instanced by a lot of other classes, so inside Listener I would to know who is the “father”;
i don’t want to use
if(objectA instanceOf Class)
....
but i think something like this:
if(this.instanceOf Class1)
System.out.println("Hello i'm the class Listener instanced by Class1);
else if(this.instanceOf Class2)
System.out.println("Hello i'm the class Listener instanced by Class2);
Is this possible or am I idiot? 😉
thanks
Nicola
If I understand correctly, you want to know what classes (and instances of those classes) contain references to instances of your Listener class.
One possible solution is to create an interface (such as ListenerHolder) which defines the methods all “listener holders” must have and accept an instance of it in your Listener’s constructor.
With that said, perhaps you can explain what the real problem is you are trying to solve. I suspect that there are other possible solutions to the original problem that you should consider.