field1.get() - returns the value of field.We then used various methods of the Field class: Here, we are accessing the public field of the Dog class and assigning it to the object field1 of the Field class. Notice the statement, Field field1 = obj.getField("type") In the above example, we have created a class named Dog. String modifier1 = Modifier.toString(mod) get the access modifier of the field type String typeValue = (String) field1.get(d1) Like methods, we can also inspect and modify different fields of a class using the methods of the Field class. To learn more, visit the Java Method class (official Java documentation). The Method class also provides various other methods that can be used to inspect methods at run time. m.getReturnType() - returns the return type of methods.m.getModifiers() - returns the access modifier of methods in integer form. m.getName() - returns the name of a method.Here, the getDeclaredMethod() returns all the methods present inside the class.Īlso, we have created an object m of the Method class. Notice the expression, Method methods = obj.getDeclaredMethod() As mentioned earlier, we have first created an object obj of Class using the getClass() method. In the above example, we are trying to get information about the methods present in the Dog class. ("Modifier: " + Modifier.toString(modifier)) Method methods = obj.getDeclaredMethods() The Method class provides various methods that can be used to get information about the methods present in a class. Constructor class - provides information about constructors in a class.Field class - provides information about fields in a class.Method class - provides information about methods in a class.The package provides classes that can be used for manipulating class members. Reflecting Fields, Methods, and Constructors Note: We are using the Modifier class to convert the integer access modifier to a string. To learn more about Class, visit Java Class (official Java documentation). obj.getSuperclass() - returns the super class of the class.obj.getModifiers() - returns the access modifier of the class.obj.getName() - returns the name of the class.Using the object, we are calling different methods of Class. Here, we are creating an object obj of Class using the getClass() method. Notice the statement, Class obj = d1.getClass() Here, we are trying to inspect the class Dog. In the above example, we have created a superclass: Animal and a subclass: Dog. String mod = Modifier.toString(modifier) put this class in different Dog.java file We can use this object to get information about the corresponding class at runtime.Įxample: Java Class Reflection import Now that we know how we can create objects of the Class. class extension // create an object of Class Here, we are using the object of the Dog class to create an object of Class.ģ. Using getClass() method // create an object of Dog class Here, the forName() method takes the name of the class to be reflected as its argument.Ģ. There exists three ways to create objects of Class:ġ. In order to reflect a Java class, we first need to create an object of Class.Īnd, using the object we can call various methods to get information about methods, fields, and constructors present in a class. The object of Class can be used to perform reflection. There is a class in Java named Class that keeps all the information about objects and classes at runtime. In Java, reflection allows us to inspect and manipulate classes, interfaces, constructors, methods, and fields at run time.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |