Use Modifier # isStatic to determine if the [Reflection] method is static.

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

Use Modifier # isStatic to determine if the [Reflection] method is static.
How to use the link_to method
How to use the include? method
How to use the form_with method
Resultset's next () is not a "method to determine if there is a ResultSet next".
[Rails] How to use the map method
[Java] How to use the toString () method
Get the type of an array element to determine if it is an array
Resultset's next () was misunderstood as a method to determine if there is a next
When you want to use the method outside
Output of how to use the slice method
How to use the replace () method (Java Silver)
[Ruby basics] How to use the slice method
If it is Ruby, it is efficient to make it a method and stock the processing.
Program to determine if it is a leap year
I want to call the main method using reflection
How to use the getter / setter method (in object orientation)
To you who lament that Java's main method is static
I want to use the sanitize method other than View.
What to do if you can't use the rails command
What is the pluck method?
[java] Reasons to use static
20 Corresponds to static method invocation
Easy to use array.map (&: method)
What is the initialize method?
Static function to check if the RGB error of BufferdImage is within the specified ratio in Java
If you want to mock a method in RSpec, you should use the allow method for mock and the singleton method.
[Ubuntu 20.04] What to do if the external monitor is not recognized
I tried to understand how the rails method "redirect_to" is defined
What to do if Cloud9 is full in the Rails tutorial
What to do if the Eclipse Maven dependency Jar is wrong
I tried to understand how the rails method "link_to" is defined
[Rails] Don't use the select method just to narrow down the columns!
Determine if the strings to be compared are the same in Java
[Ruby on Rails] Use the resources method to automatically create routes.
Function is very easy to use
[Java] How to use join method
How to use the wrapper class
Let's use reflection Private method access
[Ruby] How to use any? Method
I tried to explain the method
How to use Ruby inject method
What to do if the background image is not applied after deployment
What to do if the prefix c is not bound in JSP
Use the permutation method to make all the default users follow each other
[Ruby] What happens if the method self is used as the return value?
Is Ruby's alias method the same no matter which one you use?
If the HTTP method is not specified, the browser side requests with GET
Is it possible to put the library (aar) in the Android library (aar) and use it?