Java Reflection method get method modifiers

Introduction

We can apply several modifiers (public, private, protected, static, abstract synchronized, final, native and strictfp) to a method.

The getModifiers() method of Method class returns an encoded integer representing the set of modifiers used to the method.

The Modifier class has a set of methods to identify what modifiers the returned integer represent.

The following program lists all public methods of a given class:


import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Main {
   public static void main(String args[]) throws Exception {
      Class c = Class.forName("java.lang.String");
      Method[] methods = c.getDeclaredMethods();
      System.out.println("Public methods of:");
      for (Method m : methods)
         if (Modifier.isPublic(m.getModifiers()))
            System.out.println(m.getName());
   }/*from w  w  w .j av a  2  s  .c  o m*/
}



PreviousNext

Related