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


Advertisements

Description

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

Declaration

Following is the declaration for java.lang.reflect.Field.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.Field.getModifiers() method.

package com.tutorialspoint;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class FieldDemo {

   public static void main(String[] args) throws NoSuchFieldException, 
      SecurityException, IllegalArgumentException, IllegalAccessException {
            
      Field field = SampleClass.class.getField("sampleField");
      System.out.println("Modifier: " + Modifier.toString(field.getModifiers()));
   }
}

class SampleClass {
   public static long sampleField = 5;
}

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

public static
java_reflect_field.htm
Advertisements