Open In App

java.lang.reflect.Modifier Class in Java

Last Updated : 09 Mar, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

Java




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

Java




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

Java




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

Java




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

Java




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

Java




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

Java




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

Java




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

Java




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

Java




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

Java




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

Java




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

Java




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

Java




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

Java




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

Java




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

Java




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


Similar Reads

java.lang.reflect.Proxy Class in Java
A proxy class is present in java.lang package. A proxy class has certain methods which are used for creating dynamic proxy classes and instances, and all the classes created by those methods act as subclasses for this proxy class. Class declaration: public class Proxy extends Object implements SerializableFields: protected InvocationHandler hIt han
4 min read
java.lang.reflect.Parameter Class in Java
java.lang.reflect.Parameter class provides Information about method parameters, including their name and modifiers. It also provides an alternate means of obtaining attributes for the parameter. Some useful methods of Parameter class are: public int getModifiers(): It returns the modifier flags for the parameter represented by this Parameter object
4 min read
java.lang.reflect.Constructor Class in Java
java.lang.reflect.Constructor class is used to manage the constructor metadata like the name of the constructors, parameter types of constructors, and access modifiers of the constructors. We can inspect the constructors of classes and instantiate objects at runtime. The Constructor[] array will have one Constructor instance for each public constru
4 min read
java.lang.reflect.Method Class in Java
java.lang.reflect.Method class provides necessary details about one method on a certain category or interface and provides access for the same. The reflected method could also be a category method or an instance method (including an abstract method). This class allows broadening of conversions to occur when matching the actual parameters to call th
3 min read
java.lang.reflect.ReflectPermission Class in Java
ReflectPermission class extends BasicPermission class. It is a “named” permission i.e it contains a name but no action. It may implement actions on top of BasicPermission, if desired. It is used to get information about the behaviour of Constructors. ConstructorsDescriptionReflectPermission(String name)It is used to create a ReflectPermission with
2 min read
java.lang.reflect.Field Class in Java
The ability of the software to analyze itself is known as Reflection. This is provided by the java.lang.reflect package and elements in Class .Field serves the same purpose as the whole reflection mechanism, analyze a software component and describe its capabilities dynamically, at run time rather than at compile time .Java, like many other languag
5 min read
Class Loading and Static Blocks Execution Using Static Modifier in Java
Static is a keyword which when attached to the method, variable, Block makes it Class method, class variable, and class Block. You can call a static variable/method using ClassName. JVM executes the static block at “CLASS LOADING TIME” Execution Order: There is an order in which static block/method/variable gets initialized. Static BlockStatic Vari
3 min read
Java.lang.Class class in Java | Set 1
Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It has no public constructor. Class objects are cons
15+ min read
Java.lang.Class class in Java | Set 2
Java.lang.Class class in Java | Set 1 More methods: 1. int getModifiers() : This method returns the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and interface. These modifiers are already decoded in Mo
15+ min read
Modifier isNative(mod) method in Java with Examples
The isNative(mod) method of java.lang.reflect.Modifier is used to check if the integer argument includes the native modifier or not. If this integer parameter represents native type Modifier then method returns true else false. Syntax: public static boolean isNative(int mod) Parameters: This method accepts a integer names as mod represents a set of
2 min read
Modifier isFinal(mod) method in Java with Examples
The isFinal(mod) method of java.lang.reflect.Modifier is used to check if the integer argument includes the final modifier or not. If this integer parameter represents final type Modifier then method returns true else false. Syntax: public static boolean isFinal(int mod) Parameters: This method accepts a integer names as mod represents a set of mod
2 min read
Modifier isInterface(mod) method in Java with Examples
The isInterface(mod) method of java.lang.reflect.Modifier is used to check if the integer argument includes the interface modifier or not. If this integer parameter represents interface type Modifier then method returns true else false. Syntax: public static boolean isInterface(int mod) Parameters: This method accepts a integer names as mod represe
2 min read
Modifier isProtected(mod) method in Java with Examples
The isProtected(mod) method of java.lang.reflect.Modifier is used to check if the integer argument includes the protected modifier or not. If this integer parameter represents protected type Modifier then method returns true else false. Syntax: public static boolean isProtected(int mod) Parameters: This method accepts a integer names as mod represe
2 min read
Modifier isPrivate(mod) method in Java with Examples
The isPrivate(mod) method of java.lang.reflect.Modifier is used to check if the integer argument includes the private modifier or not. If this integer parameter represents private type Modifier then method returns true else false. Syntax: public static boolean isPrivate(int mod) Parameters: This method accepts a integer names as mod represents a se
2 min read
Modifier isStatic(mod) method in Java with Examples
The isStatic(mod) method of java.lang.reflect.Modifier is used to check if the integer argument includes the static modifier or not. If this integer parameter represents static type Modifier then method returns true else false. Syntax: public static boolean isStatic(int mod) Parameters: This method accepts a integer names as mod represents a set of
2 min read
Modifier isPublic(mod) method in Java with Examples
The isPublic(mod) method of java.lang.reflect.Modifier is used to check if the integer argument includes the public modifier or not. If this integer parameter represents public type Modifier then method returns true else false. Syntax: public static boolean isPublic(int mod) Parameters: This method accepts a integer names as mod represents a set of
2 min read
Modifier isAbstract(mod) method in Java with Examples
The isAbstract(mod) method of java.lang.reflect.Modifier is used to check if the integer argument includes the abstract modifier or not. If this integer parameter represents abstract type Modifier then method returns true else false. Syntax: public static boolean isAbstract(int mod) Parameters: This method accepts a integer names as mod represents
2 min read
Modifier isStrict(mod) method in Java with Examples
The isStrict(mod) method of java.lang.reflect.Modifier is used to check if the integer argument includes the strictfp modifier or not. If this integer parameter represents strictfp type Modifier then method returns true else false. Syntax: public static boolean isStrict(int mod) Parameters: This method accepts a integer names as mod represents a se
2 min read
Modifier isSynchronized(mod) method in Java with Examples
The isSynchronized(mod) method of java.lang.reflect.Modifier is used to check if the integer argument includes the synchronized modifier or not. If this integer parameter represents synchronized type Modifier then method returns true else false. Syntax: public static boolean isSynchronized(int mod) Parameters: This method accepts a integer names as
2 min read
Modifier isTransient(mod) method in Java with Examples
The isTransient(mod) method of java.lang.reflect.Modifier is used to check if the integer argument includes the transient modifier or not. If this integer parameter represents transient type Modifier then method returns true else false. Syntax: public static boolean isTransient(int mod) Parameters: This method accepts a integer names as mod represe
2 min read
Modifier isVolatile(mod) method in Java with Examples
The isVolatile(mod) method of java.lang.reflect.Modifier is used to check if the integer argument includes the volatile modifier or not. If this integer parameter represents volatile type Modifier then method returns true else false. Syntax: public static boolean isVolatile(int mod) Parameters: This method accepts integer mod as parameter which rep
2 min read
Public vs Protected Access Modifier in Java
Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by using an appropriate keyword in java called access mo
4 min read
Public vs Protected vs Package vs Private Access Modifier in Java
Whenever we are writing our classes we have to provide some information about our classes to the JVM like whether this class can be accessible from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not etc. we can specify this information by using an appropriate keyword in java called access mo
7 min read
Private vs Final Access Modifier in Java
Whenever we are writing our classes we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not etc. we can specify this information by using an appropriate keyword in java called access modi
3 min read
Private vs Protected vs Final Access Modifier in Java
Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by using an appropriate keyword in java called access mo
5 min read
Protected vs Final Access Modifier in Java
Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by using an appropriate keyword in java called access mo
5 min read
Abstract vs Public Access Modifier in Java
Access Modifier in Java is the reserved keyword used to define the scope of a class, variable, and methods. It also tells us about that whether child class creation is possible or not or whether object creation is possible or not. Abstract Access Modifier is a modifier applicable only for classes and methods but not for variables. If we declare any
4 min read
Java Error - All illegal Modifier Combinations For Methods w.r.t Abstract
In Java, we can apply a variety of modifiers to classes, methods, blocks, and variables and each one has a different use case and all of these primarily define how and where those classes, methods, blocks, and variables can be accessed and modified. But not all the modifiers can be applied simultaneously due to some contradiction among their defini
6 min read
Java - Final vs Static Access Modifier
Final keyword is used in different contexts. First of all, final is a non-access modifier applicable only to a variable, a method, or a class. Following are different contexts where final is used. While the static keyword in Java is mainly used for memory management. The static keyword in Java is used to share the same variable or method of a given
5 min read
Output of Java programs | Set 24 (Final Modifier)
Difficulty level : Easy Prerequisite : final keyword in java Predict the output of following Java Programs: What will be output of following program? class Test { final int MAXIMUM; final double PI; public Test(int max) { MAXIMUM = max; } public Test(double pi) { PI = pi; } public static void main(String[] args) { Test t1 = new Test(1500); Test t2
3 min read
three90RightbarBannerImg