![]() We can access runtime features of any object using a special class called Class. When a Java program runs, Java Runtime System keeps track of type information of all the object running on JVM that, the class to which each object belongs, using which the JVM can call appropriate methods on each object. classObject is a reference of type Class, which points to the class class and using that object invoked all the methods defined inside that class and printed to the console as shown below. This program used two special reference types, Class and Method, that will be discussed shortly. Method methods = classObject.getDeclaredMethods() import .* Ĭlass classObject // Reference of type ClassĬlassObject = Class.forName("") // create an object of type Class which represent "" class As part of testing purpose or code analysis have you ever been asked to programmatically extract the meta-information of a class, like what are the modifiers used in the class definition, what are the constructors, fields, and methods defined inside that class, and so on? This can be achieved using the Java Reflection API.įor instance, you have been asked to write a program to print all the methods defined inside class. You must have used different classes and interfaces from java class libraries or from different third party APIs. I hope you are experienced in writing Java classes, creating objects as well as invoking methods on an object. In Java, Reflection is a powerful and advanced feature that enables the programmers to inspect or modify the classes, interfaces, and methods of a program running on Java Virtual Machine. In Computer Programming, reflection is the process of examining and modifying the runtime behavior of applications. Reading time: 45 minutes | Coding time: 15 minutes
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |