java.lang.reflect.Field Class in Java - GeeksforGeeks

09 Mar, 2021
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 languages, is statically typed. Reflection mechanisms allow one to bypass it to some degree, and introduce some more dynamic features, like, say, retrieval of the value of the field by name. The package java.lang.reflect includes several interfaces. Of special interest is Member which defines methods that allow getting information about a field, constructor or method of a class. There are also ten classes in this package i.e. AccessibleObject, Array, Constructor, Executable, Field, Method, Modifier, Parameter, Proxy, ReflectPermission.
The following application illustrates a simple use of java reflection. It prints the fields of the class java.awt.Dimension. The program begins with forName() method of Class to get a class object for java.awt.Dimension. Once this is obtained, getFields() is used to analyze the class object. They return an array of Field objects that provide information about the object.
Example 1:
Java
import java.lang.reflect.Field;     public class GFG {         public static void main(String[] args)         throws Exception     {             // Create the User class object         User user = new User();             // Get the all field objects of User class         Field[] fields = User.class.getFields();             for (int i = 0; i < fields.length; i++) {                 // get value of the fields             Object value = fields[i].get(user);                 // print result             System.out.println("Value of Field "                               + fields[i].getName()                                + " is " + value);         }     } }     // sample User class class User {         public static String name = "Dipsundar";         public static String getName()     {         return name;     }         public static void setName(String name)     {         User.name = name;     } }
Value of Field name is Dipsundar
Example 2:
Java
import java.lang.reflect.Field;  public class GFG {      public static void main(String[] args) throws Exception    {          // Create the User class object        User user = new User();          // Get the all field objects of User class        Field[] fields = User.class.getFields();          for (int i = 0; i < fields.length; i++) {              // get value of the fields            Object value = fields[i].get(user);              // print result            System.out.println("Value of Field "                               + fields[i].getName()                               + " is " + value);        }    }}  // sample User classclass User {      public static String name = "Dipsundar";      public static String getName() { return name; }      public static void setName(String name)    {        User.name = name;    }}
Value of Field booleanValue is false
java-lang-reflect-package
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways of Reading a text file in Java
Stream In Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
HashMap get() Method in Java
Comparator Interface in Java with Examples
Strings in Java
StringBuilder Class in Java with Examples