Modifiers toString() method in Java with Examples

The toString() method of java.lang.reflect.Modifier class is used to get a string representing the access modifier flags in the specified modifier.we have to pass int value as paratmeter to get access modifier names.

Syntax:

public static String toString(int mod)

Parameters: This method accepts one parameter mod which represents a set of modifiers.



Return: This method returns a string representation of the set of modifiers represented by mod.

Below programs illustrate toString() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// toString() method
  
import java.lang.reflect.*;
  
public class GFG {
  
    public static void main(String[] args)
        throws NoSuchFieldException,
               SecurityException
    {
  
        // get Modifier value
        // of String class
        int result
            = String.class.getModifiers();
  
        // apply toString() methods
        System.out.println(
            "Modifiers: "
            + Modifier.toString(result));
    }
}

chevron_right


Output:

Modifiers: public final

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate toString()
  
import java.lang.reflect.*;
  
public class GFG {
  
    public static void main(String[] args)
        throws NoSuchFieldException,
               SecurityException
    {
  
        // printing the Modifiers name
        // between integer value 10 to 20
  
        for (int i = 10; i < 20; i++) {
  
            // apply toString() methods
            System.out.println(
                "i: "
                + i + " Modifier:"
                + Modifier.toString(i));
        }
    }
}

chevron_right


Output:

i: 10 Modifier:private static
i: 11 Modifier:public private static
i: 12 Modifier:protected static
i: 13 Modifier:public protected static
i: 14 Modifier:protected private static
i: 15 Modifier:public protected private static
i: 16 Modifier:final
i: 17 Modifier:public final
i: 18 Modifier:private final
i: 19 Modifier:public private final

References: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Modifier.html#toString()



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.