In Java programming, when you try to do something a little complicated, reflection always raises your head. I think that you often write a process that uses reflection to get a list of methods of a certain class, but if you want to judge whether the obtained method is static or not, Modifier # isStatic Use __. Below is a sample.
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class StaticTest {
    
    public static void main(String[] args) {
        Method[] methods = Sample.class.getDeclaredMethods();
        for (Method method : methods) {
            if (Modifier.isStatic(method.getModifiers())) {
                System.out.println(method.getName());
                // => privateStaticMethod publicStaticMethod
            }
        }
    }
    
    public static final class Sample {
        public static void publicStaticMethod() {};
        private static void privateStaticMethod() {};
        public void publicInstanceMethod(){};
        private void privateInstanceMethod(){};
    }
}
Reference: "Modifier (Java Platform SE 8)"
Recommended Posts