Open In App

Modifiers toString() method in Java with Examples

Last Updated : 04 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 a parameter 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: 

Java




// 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));
    }
}


Output:

Modifiers: public final

Program 2: 

Java




// 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));
        }
    }
}


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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads