devxlogo

Using the java.lang.reflect.Modifier Class

Using the java.lang.reflect.Modifier Class

You know that you can extract constructors, methods, and variables from a Java class file. Generally, when you use

	Field fields[] = c.getDeclaredFields( );

where c is initialized using

	Class c = Class.forName(className);

and print the fields array, you get all the elements declared in the class.

However, suppose you want to restrict the output to all fields other than those declared as private. In this case, you would use the following code, where the Modifier class is a part of the java.lang.reflect package:

if(!Modifier.isPrivate(fields[i].getModifiers( )){	System.out.println(fields[i]+"
");}

Using Modifier.isPrivate() ensures that the private variables are not printed.

The same can be used for methods as well.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist