I want to use static methods from one class (of one type),
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.
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.
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.
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.
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);
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"));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With