I have the following problem in Java:
I have a base class and a derived class and I have a method in the base class. When I call the Base’s foo method through Derived I want to get the Derived’s class. The foo method can be generic if it can be done that way.
class Base
{
static void foo()
{
// I want to get Derived class here
// Derived.class
}
}
class Derived extends Base
{
}
Derived.foo();
Thanks for your help!
David
That’s not the way that static methods work. You’ll have to implement
Derived.foo(), do whatever it is that’s special toDerived, and that method then callsBase.foo(). If you really need the type information, you could createBase.foo0(Class klass).But to be honest, any static method that needs to know that type of the class that it’s invoked on should probably be an instance method.