Getting a Class Object

MyClass a = new MyClass();
											a.getClass();

The most basic thing you usually do when doing reflective programming is to get a Class object. Once you have a Class object instance, you can obtain all sorts of information about the class and even manipulate the class. In this phrase, we use the getClass() method to get a Class object. This method of getting a Class object is often useful in situations in which you have the object instance but do not know what class it is an instance of.

There are several other ways of obtaining a Class object. If you have a class for which the type name is known at compile time, there is an even easier way of getting a class instance. You simply use the compiler keyword .class, as shown here: ...

Get Java™ Phrasebook 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.