Discovering the Name and Attributes of a Class
The first step to find out anything about a class or interface is to retrieve its Class object. There are three main ways to obtain a Class object:
Use the getClass() method on an instance of an unknown class—for example, Class class = someInstance.getClass();
Use the .class suffix on a known class—for example, Class class = java.awt.TextEvent.class;
Use the Class.forName method with a String that contains the name of the class—for example, Class class = Class.forName(stringNameOfClass);
The best approach to use depends upon whether the class is known at compile time (use the .class suffix), is known only at runtime (use Class.forName()), or is a mystery object reference (getClass()).
Using Reflection ...
Get PURE Java™ 2 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.