Open In App

java.lang.reflect.Modifier Class in Java

The java.lang.reflect.Modifier class contains methods used to get information about class, member and method access modifiers. The modifiers are represented as int value with set bits at distinct positions. The int value represents different modifiers. These values are taken from the tables in sections 4.1, 4.4, 4.5, and 4.7 of The JVM Specification.

Class declaration:



Public class modifier extends Object

Fields:

Field Description
ABSTRACT  Integer value representing the abstract modifier.
FINAL  Integer value representing the final modifier.
INTERFACE  Integer value representing the interface modifier.
NATIVE  Integer value representing the native modifier.
PRIVATE  Integer value representing the private modifier.
PROTECTED  Integer value representing the protected modifier.
PUBLIC  Integer value representing the public modifier.
STATIC  Integer value representing the static modifier.
STRICT  Integer value representing the strictfp modifier.
SYNCHRONIZED  Integer value representing the synchronized modifier.
TRANSIENT  Integer value representing the transient modifier.
VOLATILE  Integer value representing the volatile modifier.

Constructor:



public Modifier(): Default constructor

Methods:

Method Description
classModifiers() This method returns an int value OR-ing together the source language modifiers that can be applied to a class.
constructorModifiers()  This method returns an int value OR-ing together the source language modifiers that can be applied to a constructor.
fieldModifiers()  This method returns an int value OR-ing together the source language modifiers that can be applied to a field.
interfaceModifiers()  This method returns an int value OR-ing together the source language modifiers that can be applied to an interface.
isAbstract(int mod) This method returns true if the integer argument includes the abstract modifier, false otherwise.
isFinal(int mod)  This method returns true if the integer argument includes the final modifier, false otherwise.
isInterface(int mod)  This method returns true if the integer argument includes the interface modifier, false otherwise.
isNative(int mod)  This method returns true if the integer argument includes the native modifier, false otherwise.
isPrivate(int mod)  This method returns true if the integer argument includes the private modifier, false otherwise.
isProtected(int mod)  This method returns true if the integer argument includes the protected modifier, false otherwise.
isPublic(int mod)  This method returns true if the integer argument includes the public modifier, false otherwise.
isStatic(int mod)  This method returns true if the integer argument includes the staticfp modifier, false otherwise.
isSynchronized(int mod)  This method returns true if the integer argument includes the synchronized modifier, false otherwise.
isTransient(int mod)  This method returns true if the integer argument includes the transient modifier, false otherwise.
isVolatile(int mod)  This method returns true if the integer argument includes the volatile modifier, false otherwise.
methodModifiers()  This method returns an int value OR-ing together the source language modifiers that can be applied to a method.
parameterModifiers()  This method returns an int value OR-ing together the source language modifiers that can be applied to a parameter.
toString(int mod)  This method returns a string describing the access modifier flags in the specified modifier.

1. static int classModifiers(): 

This method returns an int value after performing OR operation on the access modifiers that can be applied to a class.

Return type: Integer




// Implementation of classModifiers() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String[] args)
    {
        System.out.println(Modifier.classModifiers());
        System.out.println(
            Modifier.toString(Modifier.classModifiers()));
    }
}

Output
3103
public protected private abstract static final strictfp

2. static int constructorModifiers(); Returns an int value after performing OR operation on the access modifiers that can be applied to a constructor.

Return type: Integer




// Implementation of constructorModifiers() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String[] args)
    {
        System.out.println(Modifier.constructorModifiers());
        System.out.println(Modifier.toString(
            Modifier.constructorModifiers()));
    }
}

Output
7
public protected private

3. static int fieldModifiers(): Returns an int value after performing OR operation on the access modifiers that can be applied to a field.

Return type: Integer




// Implementation of fieldModifiers() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String[] args)
    {
        System.out.println(Modifier.fieldModifiers());
        System.out.println(Modifier.toString(
            Modifier.fieldModifiers()));
    }
}

Output
223
public protected private static final transient volatile

4. static int interfaceModifiers(): Returns an int value after performing OR operation on the access modifiers that can be applied to an interface.

Return type: Integer




// Implementation of interfaceModifiers() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String[] args)
    {
        System.out.println(Modifier.interfaceModifiers());
        System.out.println(Modifier.toString(
            Modifier.interfaceModifiers()));
    }
}

Output
3087
public protected private abstract static strictfp

6. static boolean isAbstract(): Checks whether the integer argument includes the abstract modifier.

Return type: boolean




// Implementation of isAbstract() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        System.out.println(Modifier.isAbstract(
            demoClass.class.getModifiers()));
    }
    abstract class demoClass {
    }
}

Output
true

7. static boolean isFinal(): Checks whether the integer argument includes the abstract modifier.

Return type: boolean




// Implementation of isFinal() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        System.out.println(Modifier.isFinal(
            demoClass.class.getModifiers()));
    }
    final class demoClass {
    }
}

Output
true

8. static boolean isInterface(): Checks whether the integer argument includes the abstract modifier.

Return type: boolean




// Implementation of isInterface() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        System.out.println(Modifier.isInterface(
            demoInterface.class.getModifiers()));
    }
    interface demoInterface {
        String demoMethod();
    }
}

Output
true

9. static boolean isNative(): Checks whether the integer argument includes the abstract modifier.

Return type: boolean




// Implementation of isNative() Method
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
  
public class GFG0 {
    public static void main(String args[])
    {
        Method methods[] = demoClass.class.getMethods();
        System.out.println(
            Modifier.isNative(methods[0].getModifiers()));
    }
    static class demoClass {
        public static String demoField;
  
        public final native String getDemoField();
    }
}

Output
true

10. static boolean isPrivate(): Checks whether the integer argument includes the abstract modifier.

Return type: boolean




// Implementation of isPrivate() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        System.out.println(Modifier.isPrivate(
            demoClass.class.getModifiers()));
    }
    private class demoClass {
    }
}

Output
true

11. static boolean isProtected(): Checks whether the integer argument includes the abstract modifier.

Return type: boolean




// Implementation of isProtected() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        System.out.println(Modifier.isProtected(
            demoClass.class.getModifiers()));
    }
    protected class demoClass {
    }
}

Output
true

12. static boolean isPublic(): Checks whether the integer argument includes the abstract modifier.

Return type: boolean




// Implementation of isPublic() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        System.out.println(Modifier.isPublic(
            demoClass.class.getModifiers()));
    }
    public class demoClass {
    }
}

Output
true

13. static boolean isStatic(): Checks whether the integer argument includes the abstract modifier.

Return type: boolean




// Implementation of isStatic() Method
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        System.out.println(Modifier.isStatic(
            demoClass.class.getModifiers()));
    }
    static class demoClass {
    }
}

Output
true

14. static boolean isStrict(): Checks whether the integer argument includes the abstract modifier.

Return type: boolean




// Implementation of isStrict() Method
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        Method methods[] = demoClass.class.getMethods();
        System.out.println(
            Modifier.isStrict(methods[0].getModifiers()));
    }
    static class demoClass {
        public static String demoField;
  
        public final native String getDemoField();
    }
}

Output
false

15. static boolean isSynchronized(): Checks whether the integer argument includes the abstract modifier.

Return type: boolean




// Implementation of isSynchronized() Method
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[])
    {
        Method methods[] = demoClass.class.getMethods();
        System.out.println(Modifier.isSynchronized(
            methods[0].getModifiers()));
    }
    static class demoClass {
        public static String demoField;
  
        public final synchronized String getDemoField()
        {
            return demoField;
        }
    }
}

Output
true

16. static boolean isTransient(): Checks whether the integer argument includes the abstract modifier.

Return type: boolean




// Implementation of isTransient() Method
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[]) throws Exception
    {
        Field field
            = demoClass.class.getDeclaredField("demoField");
        System.out.println(
            Modifier.isTransient(field.getModifiers()));
    }
    static class demoClass {
        public transient String demoField;
  
        public final native String getDemoField();
    }
}

Output
true

17. static boolean isVolatile(): Checks whether the integer argument includes the abstract modifier.

Return type: boolean




// Implementation of isVolatile() Method
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
  
public class GFG {
    public static void main(String args[]) throws Exception
    {
        Field field
            = demoClass.class.getDeclaredField("demoField");
        System.out.println(
            Modifier.isVolatile(field.getModifiers()));
    }
    static class demoClass {
        public volatile String demoField;
  
        public final native String getDemoField();
    }
}

Output
true

18. static int methodModifiers(): Returns an int value after performing OR operation on the values of access modifiers that can be applied to a method.

Return type: Integer




// Implementation of methodModifiers() Method
import java.lang.reflect.Modifier;
  
public class modifier {
    public static void main(String[] args)
    {
        System.out.println(Modifier.methodModifiers());
    }
}

Output
3391

Article Tags :