1

We have class library as

class A {
    public void callWorkflow()  {
      B b = new B();
    }
}

class B {
    public void callStatic() {
      C.someMethod();
    }
}

class C {
    public static someMethod() {}
}

We are actually trying to change functionality of static method someMethod. Is there a way to solve this problem without changing call hierarchy?

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
venkat g
  • 421
  • 1
  • 6
  • 20
  • You might find your answer in [Why doesn't Java allow overriding of static methods?](http://stackoverflow.com/q/2223386/1072229) – Grisha Levit Jan 24 '17 at 07:08

2 Answers2

3

You can't just Override a static method. In my opinion, remove static from the method someMethod(), then create an object of class C inside class B. Then call the method.

Class A{
    public void callWorkflow()  {
      B b = new B();}
}
Class B{
    public void callStatic(){
      C c = new C();
      c.someMethod();}
}
Class C{
    public someMethod(){}
}
ThisaruG
  • 3,222
  • 7
  • 38
  • 60
1

There is no way to override a static method.

That's why one of these approaches is preferred to calling static methods:

  • Inject another object (service) that will provide the functionality in a non-static method and call it through the injected object
  • Make the static method a thin wrapper that just delegates the work to some non-static object that can be configured (like in slf4j's logger)
Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43