Saturday, July 11, 2026
HomeLanguagesJavaField getModifiers() method in Java with Examples

Field getModifiers() method in Java with Examples

The getModifiers () method of java.lang.reflect.Field used to return the modifiers used for the field object as time of declaration, as an integer. The Modifier class should be used to decode the modifiers. Syntax:

public int getModifiers()

Parameters: This method accepts nothing. Return: This method returns the Java language modifiers for the underlying member. Below programs illustrate getModifiers () method: Program 1: 

Java




// Java program to illustrate
// getModifiers () method
 
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
 
public class GFG {
 
    // initialize field
    private static int number;
 
    public static void main(String[] args)
        throws NoSuchFieldException
    {
        // get Field object
        Field field
            = GFG.class
                  .getDeclaredField("number");
 
        // apply getModifiers() method
        int modifiers
            = field.getModifiers();
 
        // print the results
        System.out.println(
            Modifier
                .toString(modifiers));
    }
}


Output:

private static

Program 2: 

Java




// Java program to illustrate
// getModifiers () method
 
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
 
public class GFG {
 
    // initialize field
    final static String value
        = "Geeks";
 
    public static void main(String[] args)
        throws NoSuchFieldException
    {
 
        // get Field object
        Field field
            = GFG.class
                  .getDeclaredField("value");
 
        // apply getModifiers() method
        int modifiers
            = field.getModifiers();
 
        // print the results
        System.out.println(
            Modifier
                .toString(modifiers));
    }
}


Output:

static final

References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getModifiers–

RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32519 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6901 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12111 POSTS0 COMMENTS
Shaida Kate Naidoo
7021 POSTS0 COMMENTS
Ted Musemwa
7263 POSTS0 COMMENTS
Thapelo Manthata
6978 POSTS0 COMMENTS
Umr Jansen
6968 POSTS0 COMMENTS