Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send static methods as parameter and generically call within another method in Java

I want to use static methods from one class (of one type),

  1. send them to another method in a different class, and
  2. call them within the different class's method without explicitly using their names

so as to make the call generic.

Is there any way to achieve this functionality? Some pseudo-code that helps exemplify what I'm trying to do:

public class A
{
     public static void aMethod1()
     {
          //do something
     }

     public static void aMethod2()
     {
          //do something else
     }
}

public class B
{
     public void bMethod(<Parameter that can take any of A's methods polymorphically>)
     {
          <call ANY method sent in by the parameter with the same line of code>
     }
}

public class Main
{
     public static void main(String[] args)
     {
          A obj_A = new A();
          B obj_B = new B();
          obj_B.bMethod(<Send any of A's methods>);
     }
}

I may be on the completely wrong path but this is how I imagine it might be done. Thanks for your help in advance.

like image 627
squiglert Avatar asked Jan 18 '16 21:01

squiglert


People also ask

Can static methods call other methods?

A static method can call only other static methods; it cannot call a non-static method. A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables.

Can a static method call another static method Java?

Static method can call another static method simply by its name within same class. Static method can call another non staic method of same class only after creating instance of the class. Non static method can call another static method of same class simply by way of classname.

How do you call a static method from another class in Java?

Call a static Method in Another Class in Java In the case of a static method, we don't need to create an object to call the method. We can call the static method by using the class name as we did in this example to call the getName() static method.


2 Answers

Using method references:

Declare:

public class B
{
     public void bMethod(Runnable runnable)
     {
          runnable.run();
     }
}

Now pass method references to bMethod()

new B().bMethod(A::aMethod1);
new B().bMethod(A::aMethod2);

Using reflection

Declare:

public class B {
     public void bMethod(Method method) {
          try {
              method.invoke(null)
          }
          catch (Exception e) {
              throw new RuntimeException(e);
          }
     }
}

Now pass methods to bMethod() via reflection:

new B().bMethod(A.class.getMethod("aMethod1"));
new B().bMethod(A.class.getMethod("aMethod2"));
like image 142
Lukas Eder Avatar answered Oct 06 '22 16:10

Lukas Eder


Well, you can achieve that using reflection.

public void bMethod(String name) throws NoSuchMethodException,
  InvocationTargetException,
  IllegalAccessException {
    A.class.getMethod(name).invoke(null);
}

Where name is a name of your method (be it aMethod1 or aMethod2).

However, this is not a real OOP way and you should really use some other approach instead. For example, define an interface called Behavior

interface Behavior {
  void run();
}

Then You create two different implementations of this interface.

class BehaviorA implements Behavior {
  @Override
  public void run() {
    // Behavior A
  }
}

class BehaviorB implements Behavior {
  @Override
  public void run() {
    // Behavior B
  }
}

Now your bMethod's signature inside of your B class can look as follows:

void bMethod(Behavior b);

And voila! You can pass two different behaviors to your method now, without using ugly reflection-based hacks.

like image 43
Mateusz Herych Avatar answered Oct 06 '22 18:10

Mateusz Herych