Skip to content
Related Articles

Related Articles

Modifiers toString() method in Java with Examples
  • Last Updated : 24 Sep, 2019

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:






// 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 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

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :