1

Suppose I have a base class with a function foo

public class Base
{
    protected static void foo()
    {
        // ToDo - what is the name of the child class calling me?
    }
}

and at least one child class containing a static initialiser that calls foo

public class Child extends Base
{
    static
    {
        foo();
    }
}

Is there a way of foo() knowing which child class has called it? I'm presuming there's a reflection technique I can use.

P45 Imminent
  • 8,319
  • 4
  • 35
  • 78
  • 1
    This is the same as identifying the calling class of foo() – Franzl Apr 25 '14 at 14:46
  • 1
    The technique you're looking for is described [here](http://stackoverflow.com/q/421280/1225328) (i.e. find the caller). That said, why do you need to know the caller? If it's for logging purposes, I'm sure this is the best solution actually. – sp00m Apr 25 '14 at 14:46
  • The stack trace solution suggested by @sp00m is certainly the way to go, but if you really, really need to know that in `foo`, maybe you should pass the invoking class or object as an argument to the method. – Edwin Dalorzo Apr 25 '14 at 14:52
  • Using classes as per @crownjewel82's answer is probably more reliable than reading stacktraces, but incurs an awkward parameter overhead. There is usually a better approach depending on the problem (see [this question](http://stackoverflow.com/questions/1696551)) – Roberto Apr 25 '14 at 14:58
  • I recommend seriously evaluating why the called routine needs to know what called it. Subprograms (subroutines, functions, methods, whatever you call them) are supposed to do a single job, operating on the parameters they are given and possibly returning a value. If they must know who called them in order to do their job, then they start to grow code that is specific to their callers, and this is terrible software engineering. So evaluate whether this is really necessary; what is it specifically that the method needs to do, and what does it need to do it? – arcy Apr 25 '14 at 16:11

2 Answers2

0

The simplest way is to pass an argument. For example

public class Base {
    protected static void foo(Class<?> type) {
        if (type == Child.class) {

        }
    }
}

public class Child extends Base {
   static {
       foo(Child.class);
   }
}

However, if you need to do something that depends on the child class then I recommend looking for a solution that leverages abstract methods and polymorphism.

public Base {
    protected static void foo(Base child) {
        child.doFoo();
    }

    protected abstract void doFoo();
}

public Child extends Base {
    static {
        foo(new Child());
    }

    @Override
    protected void doFoo() {
        //do the child specific thing here
    }
}
crownjewel82
  • 437
  • 2
  • 12
0

You can get the class by using the getClass method, like:

o.getClass()

also, if you have a class c and you need to check whether o is an instance of c, you can use instanceof, like this:

o instanceof c

Cheers.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175