java.lang.reflect.Modifier Class in Java
Last Updated :
09 Mar, 2021
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()));
}
}
Output3103
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()));
}
}
Output7
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()));
}
}
Output223
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()));
}
}
Output3087
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 {
}
}
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 {
}
}
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();
}
}
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();
}
}
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 {
}
}
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 {
}
}
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 {
}
}
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 {
}
}
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();
}
}
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;
}
}
}
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();
}
}
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();
}
}
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());
}
}
Similar Reads
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 oc
3 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
4 min read
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 Seria
4 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 t
5 min read
Java.lang.Number Class in Java
Most of the time, while working with numbers in java, we use primitive data types. But, Java also provides various numeric wrapper sub classes under the abstract class Number present in java.lang package. There are mainly six sub-classes under Number class.These sub-classes define some useful method
9 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 n
2 min read
java.lang.MethodType Class in Java
MethodType is a Class that belongs to java.lang package. This class consists of various types of methods that help in most of cases to find the method type based on the input object, or parameters of the method. All the instances of methodType are immutable. This means the objects of the methodType
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 hav
4 min read
Java.lang.Package Class in Java
In Java, the package class was introduced in JDK 1.2 to encapsulate version data associated with a package. As the number of packages increases, knowing the version of the package has become important. This versioning information is retrieved and made available by the ClassLoader instance that loade
9 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 in
15+ min read