1. Overview

In this quick tutorial, we’ll discuss how to invoke a static method in Java by using the Reflection API.

We’ll cover two different scenarios:

  • The static method is public.
  • The static method is private.

2. An Example Class

To make the demonstration and explanation easier, let’s first create a GreetingAndBye class as the example:

public class GreetingAndBye {

    public static String greeting(String name) {
        return String.format("Hey %s, nice to meet you!", name);
    }

    private static String goodBye(String name) {
        return String.format("Bye %s, see you next time.", name);
    }
}

The GreetingAndBye class looks pretty simple. It has two static methods, one public and one private.

Both methods accept a String argument and return a String as the result.

Now, let’s call the two static methods using the Java Reflection API. In this tutorial, we’ll address the code as unit test methods.

3. Invoking a public static Method

First, let’s see how to call the public static method:

@Test
void invokePublicMethod() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    Class<GreetingAndBye> clazz = GreetingAndBye.class;
    Method method = clazz.getMethod("greeting", String.class);

    Object result = method.invoke(null, "Eric");

    Assertions.assertEquals("Hey Eric, nice to meet you!", result);
}

We should note that we need to handle the required checked exceptions when we use the Reflection API.

In the example above, we first obtain the instance of the class we want to test, which is GreetingAndBye.

After we have the class instance, we can get the public static method object by calling the getMethod method.

Once we hold the method object, we can invoke it simply by calling the invoke method.

It’s worthwhile to explain the first argument of the invoke method. If the method is an instance method, the first argument is the object from which the underlying method is invoked.

However, when we invoke a static method, we pass null as the first argument, as static methods don’t require an instance in order to be called.

Finally, if we run the test, it’ll pass.

3. Invoking a private static Method

Invoking a private static method is pretty similar to invoking a public one. Let’s take a look at the code first:

@Test
void invokePrivateMethod() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    Class<GreetingAndBye> clazz = GreetingAndBye.class;
    Method method = clazz.getDeclaredMethod("goodBye", String.class);
    method.setAccessible(true);

    Object result = method.invoke(null, "Eric");

    Assertions.assertEquals("Bye Eric, see you next time.", result);
}

As we can see in the code above, when we try to get the Method object of a private method, we should use getDeclaredMethod instead of getMethod.

Moreover, we need to call method.setAccessible(true) to invoke a private method. This will ask the JVM to suppress the access control checks on this method.

Thus, it allows us to invoke the private method. Otherwise, an IllegalAccessException exception will be raised.

The test will pass if we execute it.

4. Conclusion

In this short article, we’ve addressed how to invoke static methods using the Java Reflection API.

As always, the complete code can be found over on GitHub.

Course – LS (cat=Java)

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.