Now that we understand both getClass()
and instanceof
, let's create a more comprehensive utility class that can print detailed type information for any Java object.
Creating a Reusable TypeInfo Utility
A well-designed utility class can make it easier to inspect objects in your code. Let's create a file named TypeInfo.java
in the java-type-printing
directory:
import java.lang.reflect.Modifier;
public class TypeInfo {
/**
* Prints detailed information about an object's type
*/
public static void printTypeInfo(Object obj) {
if (obj == null) {
System.out.println("Cannot determine type: object is null");
return;
}
Class<?> clazz = obj.getClass();
System.out.println("Type Information for: " + obj);
System.out.println("---------------------------");
System.out.println("Class name: " + clazz.getName());
System.out.println("Simple name: " + clazz.getSimpleName());
System.out.println("Package: " + clazz.getPackageName());
System.out.println("Is Array: " + clazz.isArray());
System.out.println("Is Interface: " + clazz.isInterface());
System.out.println("Is Primitive: " + clazz.isPrimitive());
// Get modifiers (public, private, final, etc.)
int modifiers = clazz.getModifiers();
System.out.println("Is Public: " + Modifier.isPublic(modifiers));
System.out.println("Is Final: " + Modifier.isFinal(modifiers));
// Get superclass
Class<?> superClass = clazz.getSuperclass();
System.out.println("Superclass: " + (superClass != null ? superClass.getName() : "none"));
// Get interfaces
Class<?>[] interfaces = clazz.getInterfaces();
System.out.print("Interfaces: ");
if (interfaces.length > 0) {
for (int i = 0; i < interfaces.length; i++) {
System.out.print(interfaces[i].getName());
if (i < interfaces.length - 1) {
System.out.print(", ");
}
}
System.out.println();
} else {
System.out.println("none");
}
}
}
Creating a Test Class for TypeInfo
Now, let's create another file called TypeInfoDemo.java
to test our utility class:
import java.util.ArrayList;
import java.util.List;
public class TypeInfoDemo {
public static void main(String[] args) {
// Test with different types of objects
String text = "Hello, TypeInfo!";
Integer number = 200;
ArrayList<String> list = new ArrayList<>();
// Print type information for different objects
TypeInfo.printTypeInfo(text);
System.out.println();
TypeInfo.printTypeInfo(number);
System.out.println();
TypeInfo.printTypeInfo(list);
System.out.println();
// Try with an array
int[] numbers = {1, 2, 3, 4, 5};
TypeInfo.printTypeInfo(numbers);
}
}
Compile and Run the Program
Compile and run the test program:
cd ~/project/java-type-printing
javac TypeInfo.java TypeInfoDemo.java
java TypeInfoDemo
You should see detailed output for each object, similar to:
Type Information for: Hello, TypeInfo!
---------------------------
Class name: java.lang.String
Simple name: String
Package: java.lang
Is Array: false
Is Interface: false
Is Primitive: false
Is Public: true
Is Final: true
Superclass: java.lang.Object
Interfaces: java.io.Serializable, java.lang.Comparable, java.lang.CharSequence
Type Information for: 200
---------------------------
Class name: java.lang.Integer
Simple name: Integer
Package: java.lang
Is Array: false
Is Interface: false
Is Primitive: false
Is Public: true
Is Final: true
Superclass: java.lang.Number
Interfaces: java.lang.Comparable, java.lang.constant.Constable, java.lang.constant.ConstantDesc
...
Understanding the Reflection API
Our TypeInfo
utility demonstrates the power of Java's Reflection API, which allows you to examine the structure of classes at runtime. The Reflection API can:
- Inspect classes, interfaces, fields, and methods
- Determine the modifiers, return types, and parameters
- Create new instances, invoke methods, and access fields
While powerful, Reflection should be used carefully as it can impact performance and may break encapsulation. However, for debugging and learning purposes, it's an excellent tool to understand Java's type system.