Class object metadata

0

In Java Reflection Example, I showed you how to invoke a method using reflection. In order to invoke a method, we must first obtain Method metaobject. If the method has parameters then apart from the method name, we should also know the classes of those parameters else we won’t be able to find the method.

Even if we want to know all the methods in a class, we need to know the Class metaobject. In this article, I will show the various ways of getting the Class object.

Using Class name

If we only know the class name the we can load the class using Class<?> clazz = Class.forName("className");
Note that if the class loader of the class to be loaded is different from that of the caller’s classloader then we need to pass the class loader as the second argument. By default, it will use the caller class’s class loader.

Through Object reference

If we only have the object reference and don’t know the class type, then object.getClass() will return us the Class object. The getClass method is in Object class so any object in java can be queried for this class. Note that getClass is a final method so one can’t override it to return wrong class.

Through static reference

If we know the name of the class, we should try accessing the class object statically. For example, if TestBean is the class, we can access it statically using .class literal which would be TestBean.class.

Primitive Class

Java primitives are not objects but still java uses class objects to represent all the eight primitives.
For example, int is represented by class int.class

Class intClass = int.class;
assertTrue(intClass.isPrimitive());

To know whether a class object that represents a primitive type, we just nee to call Class.isPrimitive(). Note that a primitive class cannot be used to create a new instance of a primitive.

void class

The keyword void is not a type in java. A method is declared with return type as viod if it does not return a value. However, java does have a class object to represent void, and it is void.class

Interface class

There is also a class object to represent each declared interface. It is just like any other class however you cannot create a new instance of it. For example, interface Collection is represented by Collection.class. An interface class object may be introspected for methods and constants but you cannot create an instance. If we have the Class object, calling isInterface will let us know whether it represents a declared interface.

Array type class

Java arrays are objects, but their classes are created by the JVM at runtime. A new class is created for each element type and dimension. For example, a single dimensional Object array is represented by Object[].class. A two-dimensional int[][] array is represented by int[][].class.
By class isArray() on Class object we will know whether the class represents an array object. The component type of an array clas can be obtained using getComponentType().

Share.

Leave A Reply