public staticvoid printClassName(T object) { System.out.println(object.getClass().getName()); } String str = "Hello World"; printClassName(str); // Output: java.lang.String
public staticIn this example, we define a generic method that takes an object of type T and returns its class object. We then call this method with a String object and assign the return value to a variable of type Class. Finally, we print the name of this class using getName. The T getName method is part of the java.lang.reflect package.Class getClass(T object) { return object.getClass(); } String str = "Hello World"; Class strClass = getClass(str); System.out.println(strClass.getName()); // Output: java.lang.String