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?

1

2 Answers 2

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(){}
}
Sign up to request clarification or add additional context in comments.

1 Comment

The Class C is in an external library, so its not possible to change implementation there.
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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.