Object .getClass() and Class Common Methods Tutorial

The Object class contains a method named .getClass() that returns an instance of type Class - that's right, there is a class named Class (sure, why not???). All kidding aside, the class Class contains over 60 methods that you can use to determine various things about an instance of an object. Every class and interface inherits this method either directly or indirectly from the Object class so we can invoke it on just about everything.
The .getClass() method takes no parameters and is super easy to invoke, just tack it on to your reference variable and you are good to go.

     Integer i = new Integer(41);
     Class c = i.getClass();
     System.out.println(c.getName()); // java.lang.Integer
     System.out.println(c.toGenericString()); // public final class java.lang.Integer

     Method mArray[] = i.getClass().getDeclaredMethods();
     for (Method m : mArray) {
         System.out.println(m.getName()); // display all the methods of the Integer class
     }

Now that you see how easy it is to invoke the .getClass() method, let's go ahead and test out some common methods of the class Class.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md GetClassMethod
C:\Java>cd GetClassMethod
C:\Java\GetClassMethod>Notepad GetClassMethod.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


import java.lang.reflect.*;
import java.util.*;

enum Silly { ONE, TWO, THREE }

class GetClassMethod {
    public static void main(String args[]) {
        //MethodTester.testMe(new Integer(41));
        //MethodTester.testMe(new int[2]);
        //MethodTester.testMe(new ArrayList());
        //MethodTester.testMe("Hello World");
        //MethodTester.testMe(Silly.TWO);
        //Runnable r = new Runnable() {
        //	public void run() {}
        //};
        //MethodTester.testMe(r);	
    }
}

class MethodTester {
    static void testMe(Object obj) {
        Class c = obj.getClass();
        System.out.println("getSimpleName() = " + c.getSimpleName());
        System.out.println("getName() = " + c.getName());
        System.out.println("toGenericString() = " + c.toGenericString());
        System.out.println("getPackage() = " + c.getPackage());
        System.out.println("getGenericSuperclass() = " + c.getGenericSuperclass().getTypeName());
        if (c.isInterface()) { System.out.println("It is an interface"); }
        if (c.isArray()) { System.out.println("It is an array"); }
        if (c.isEnum()) { System.out.println("It is an enum"); }
        if (c.isAnonymousClass()) { System.out.println("It is an anonymous class"); }

        System.out.println();
        Field fArray[] = c.getDeclaredFields();
        for(Field f : fArray) {
            System.out.println("getDeclaredFields(): " + f.getName());
        }

        Constructor cArray[] = c.getDeclaredConstructors();
        for(Constructor con : cArray) {
            System.out.println("getDeclaredConstructors() = " + con);
        }

        Method mArray[] = c.getDeclaredMethods();
        for(Method m : mArray) {
            System.out.println("getDeclaredMethods(): " + m.getName());
        }
    }
}



Now switch back to the command prompt (CMD) and type in javac GetClassMethod.java and press Enter.
Now type in java GetClassMethod and press Enter.


C:\Java\GetClassMethod>javac GetClassMethod.java
C:\Java\GetClassMethod>java GetClassMethod
message


Final thoughts


Tutorials