java.lang.reflect.Method.getModifiers() Method Example


Advertisements

Description

The java.lang.reflect.Method.getModifiers() method returns the Java language modifiers for the method represented by this Method object, as an integer. The Modifier class should be used to decode the modifiers.

Declaration

Following is the declaration for java.lang.reflect.Method.getModifiers() method.

public int getModifiers()

Returns

the Java language modifiers for the underlying member.

Example

The following example shows the usage of java.lang.reflect.Method.getModifiers() method.

package com.tutorialspoint;

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

public class MethodDemo {

   public static void main(String[] args) {

      Method[] methods = SampleClass.class.getMethods();
      for (Method method : methods) {
         System.out.println("Modifier: " + Modifier.toString(method.getModifiers()));
         System.out.println("Method: " + method.toGenericString());
      }
   }
}

class SampleClass {
   private String sampleField;

   public String getSampleField() throws ArrayIndexOutOfBoundsException {
      return sampleField;
   }

   public void setSampleField(String sampleField) {
      this.sampleField = sampleField; 
   } 
}

Let us compile and run the above program, this will produce the following result −

Modifier: public
Method: public java.lang.String com.tutorialspoint.SampleClass.getSampleField() throws java.lang.ArrayIndexOutOfBoundsException
Modifier: public
Method: public void com.tutorialspoint.SampleClass.setSampleField(java.lang.String)
Modifier: public final
Method: public final void java.lang.Object.wait() throws java.lang.InterruptedException
Modifier: public final
Method: public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
Modifier: public final native
Method: public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
Modifier: public
Method: public boolean java.lang.Object.equals(java.lang.Object)
Modifier: public
Method: public java.lang.String java.lang.Object.toString()
Modifier: public native
Method: public native int java.lang.Object.hashCode()
Modifier: public final native
Method: public final native java.lang.Class<?> java.lang.Object.getClass()
Modifier: public final native
Method: public final native void java.lang.Object.notify()
Modifier: public final native
Method: public final native void java.lang.Object.notifyAll()
java_reflect_method.htm
Advertisements