0

I need to call a superclass static method on a subclass and be aware that it was called on a subclass. The following code shows the use case, please notice the method invocation on a subclass B.importantMethod() and the place where I need to know that it was called on subclass: //would like to get B here.

class A {
    public static void importantMethod() {
        System.out.println("???"); //would like to get B here
    }
}

class B extends A {
    //no implementation, just inheritance
}

public class Test {
    public static void main(String[] args) {
        B.importantMethod();
    }
}

Any possible way of achieving this will be good for me. My tries ended up with nothing, I always get A, not B.

user2068645
  • 137
  • 2
  • 7

2 Answers2

2

This is just not possible. Static methods always belong to the class that defines them. importantMethod is a method of class A.

Nikem
  • 5,716
  • 3
  • 32
  • 59
1

static methods are class methods and thus belong to that class; there is no way to do what you want. For a further explanation, see this topic. If you provide more information about the problem you are trying address this way, may be we can help you find an alternative solution.

Community
  • 1
  • 1