1

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

Nicola
  • 465
  • 2
  • 7
  • 19

3 Answers3

1

If you're just looking for the Class of the object that called the constructor, this question might be what you're looking for. Get the stack trace in the constructor, and use it to determine which class called the constructor.

If you're looking for the specific Object reference rather than the class, you will need to pass it as a parameter.

Community
  • 1
  • 1
Mike
  • 2,434
  • 1
  • 16
  • 19
  • 2
    this should then just be a comment, suggesting that the question is a duplicate – Scorpion Jan 05 '13 at 19:18
  • Sorry, the answer was poorly worded. It may or may not be a duplicate, depending on exactly what he's asking. The second block of code doesn't really make sense in the context of the question and has left me unsure as to which question is being asked. – Mike Jan 05 '13 at 19:22
  • thank you, I think I'll pass the object as a parameter because I need also to call a method of that class – Nicola Jan 06 '13 at 18:18
1

You can do this (just remove the . - this instanceof ClassA) but it seems to me like a bad practice. If you want to have a behavior that depends on the subclass, just implement a method in each subclass and call it at this point.

For example:

public class Parent
{
    public void foo()
    {
        bar();
    }

    public void bar()
    {
        System.out.println("Parent");
    }

    public static void main()
    {
        new Parent.foo(); // prints "Parent"
        new SubA.foo();   // prints "SubA"
        new SubB.foo();   // prints "SubB"
    }
}

public class SubA extends Parent
{
    @Override public void bar()
    {
        System.out.println("SubA");
    }
}

public class SubB extends Parent
{
   @Override public void bar()
    {
        System.out.println("SubB");
    }
}
MByD
  • 135,866
  • 28
  • 264
  • 277
1

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.

public interface ListenerHolder {
    public void doSomething();
}

public class Listener {
    public Listener(ListenerHolder holder) {
        this.holder = holder;
    }

    public void doListener() {
        this.holder.doSomething();
    }

    private ListenerHolder holder;
}

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.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • thank you, I think i could use your solution :) .. I'm creating a publisher subscriber communication with javaee, so i have a Listener class with the method onMessage that receive an object and has to call another method of the class that instanced it for managing the content of the object received.. because of many classes can instance the class Listener, the method to call is different – Nicola Jan 06 '13 at 18:14
  • question: I could instead use different kind of constructors, one for each class that could instance the class Listener. which solution is best in your opinion? – Nicola Jan 06 '13 at 22:26
  • @Nicola Please provide some code to illustrate what you have in mind. I'll be glad to give some suggestions from there. – Code-Apprentice Jan 06 '13 at 23:34
  • Using different constructor it works, however now I use a Class that is extended by other classes, so I can use only one constructor with the Parent object: public Listener(Block block){ this.block=block; } – Nicola Jan 10 '13 at 18:40
  • @Nicola Is that a problem? – Code-Apprentice Jan 10 '13 at 18:46
  • @Nicola I didn't see a question in your comment about the changes you made. Is it working the way you want? – Code-Apprentice Jan 10 '13 at 19:08
  • @Nicola Thanks for accepting my answer. You should also upvote any answers that you found helpful. – Code-Apprentice Jan 10 '13 at 19:44