seriakillaz
seriakillaz

Reputation: 843

Java: get the class of the inherited class from a static method

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

Upvotes: 15

Views: 7128

Answers (5)

Hauke Ingmar Schmidt
Hauke Ingmar Schmidt

Reputation: 11607

static methods are not inheritated. Static methods with the same signature can only hide similar methods in the superclass. This means that you never will see the result you probably want - you always exactly know the enclosing class. It is never possible that the static method is somehow "within" another class. So it is just impossible to produce the desired result. Calling a static method from a subclass or an instance is a bad idea for this reason as it just hides the real class. (IDEs and static code analysis tools can mark or correct this.)

Sources:

So what works with inherited methods does not work with static methods that are not inherited.

class Base {
    static void foo() {
        // Only the static context is available here so you can't get class dynamic class information
    }
    void bar() {
        System.out.println(getClass());
    }
}
class Derived extends Base {
}
class Another extends Base {
    static void foo() {
         // No super call possible!
         // This method hides the static method in the super class, it does not override it.
    }
    void bar() {
         super.bar();
    }
}

Derived derived = new Derived();
derived.bar(); // "class Derived"
Base base = new Base();
base.bar(); // "class Base"

// These are only "shortcuts" for Base.foo() that all work...
derived.foo(); // non-static context
Derived.foo(); // could be hidden by a method with same signature in Derived 
base.foo(); // non-static context

Base.foo(); // Correct way to call the method

Another a = new Another();
a.foo(); // non-static context
Another.foo(); 

Is it good idea that the language allows this? - Hm. I think it is telling that IDEs and code analysis tools warn and can even correct this automatically.

Upvotes: 1

Miserable Variable
Miserable Variable

Reputation: 28752

Derived.foo();

This will go to foo defined in Derived, if one is defined there:

class Base {
    static void foo() {
    System.out.println("Base");
    }
}

class Der extends Base {
    static void foo() {
    System.out.println("Der");
    }
}

class Check {
    public static void main(String[] args) {
    Base.foo();
    Der.foo();
    }
}

When I run it:

javac -g Check.java && java Check 
Base 
Der

So what is your question? If you to require that each derived class implement foo that is not possible to enforce in Java.

Upvotes: -1

Raedwald
Raedwald

Reputation: 48644

Well, the caller of Derived.foo() knows what they are calling, so you could alter your methods thus:

class Base
{
    static void foo(Class< T > calledBy)
    {
        // I want to get Derived class here
        // Derived.class
    }
}

class Derived extends Base
{
}

Derived.foo(Derived.class);

Upvotes: 3

Joop Eggen
Joop Eggen

Reputation: 109547

Not possible, Derived.foo() will simply give code for Base.foo().

Upvotes: 0

parsifal
parsifal

Reputation: 131

That's not the way that static methods work. You'll have to implement Derived.foo(), do whatever it is that's special to Derived, and that method then calls Base.foo(). If you really need the type information, you could create Base.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.

Upvotes: 4

Related Questions