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 Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read