How to retrieve the component type of an array
In Java, every array is an object, regardless of whether array elements are objects or primitive types. As a consequence, every array has a corresponding java.lang.Class object, which can be retrieved by calling the getClass() method of java.lang.Object. From the Class object associated to an array, we can retrieve the type of array elements via the getComponentType()method, which returns the Class object representing the element type. Class objects can represent not only classes and interfaces, but also primitive types, so the getComponentType() method works for any array object; if the object on which this method is called does not represent an array, then the method returns null.
In the following example, we first declare two arrays, and then retrieve their component types and print them on the standard output.
How to retrieve Java interfaces by using the getInterfaces() method
In Java, an interface is a set of functionalities that an object of a given class is expected to implement. A class is said to implement an interface when it contains all the methods defined in the interface. An interface can extend another interface, thus there are hierarchies of interfaces similarly to class hierarchies. Differently from the single inheritance model characterizing relationships between classes, a class can implement more than one interface, and an interface can extend more than one interface.
The java.lang.Class class provides a method to retrieve the interfaces implemented in a given class. This method is called getInterfaces() and returns an array (possibly with zero length) of Class objects representing the different interfaces implemented by a given class. It is worth noting that Class objects can refer not only to Java classes, but also to interfaces. If a Class object represents an interface, calling getInterfaces() on it returns an array with all the interfaces extended by this interface.
How to identify array objects
In computer programs, arrays are useful to group and access multiple elements of the same type. In Java, arrays (both single- and multi-dimensional) can be created from either primitive types (such as int, float, etc.) or classes. Every array belongs to a class that contains information on the type of array elements and the number of dimensions; as with any other class, the Class object corresponding to an array can be retrieved with the getClass() method of the java.lang.Object class.
Given a Class object, we can check whether it corresponds to an array object with the isArray() method, which returns true for array objects and false otherwise. The following example demonstrates the use of this method. After creating an array of integer values and a String object, we retrieve the corresponding Class objects and then we call isArray() on these objects.
How to find the superclass name by using the getSuperclass() method
In Java, class hierarchies follow the single inheritance model, i.e. a class can have at most one superclass. The only class that does not have any superclass is java.lang.Object, while all other classes, including user-defined classes, derive (directly or indirectly) from Object. The java.lang.Class class provides a method, called getSuperclass(), to retrieve the direct superclass (i.e. the next class encountered when walking up the hierarchy) of a given class; this method returns a Class object representing the superclass of the Class object on which the method is called; if the method is called on the Object class, null is returned.
How to check whether a class has a no-argument constructor
The java.lang.Class<T> class can be used to instantiate Java objects without calling explicitly their class constructor. For example, we can load a class by specifying its fully qualified name (i.e. the class name preceded by the name of the package in which the class is defined), using the forName(String className) static method of Class. If the given class can be found by the class loader, this method returns a Class object corresponding to the given class, otherwise it throws a java.lang.ClassNotFoundException. To instantiate an object of the returned class, we can use the newInstance() method, which returns an object created with the default constructor, i.e. the no-argument constructor. If the class or its constructor are not accessible form the calling code, a java.lang.IllegalAccessException is thrown, and if the instantiation fails for other reasons (for example, if the class does not have a no-argument constructor), a java.lang.InstantiationException is thrown.