Java: Class class

Like a human thinking about his/her own thinking, the process of a running program examining its classes is called reflection or introspection.

There is run-time information about classes that you can access. The central class representing this information is java.lang.Class, a class confusingly named Class.

Use the Object getClass() method to get a Class object

Every class has the class Object as an ancestor. A reflection method in the Object class is getClass(), which returns the Class of that object.

Class methods

java.lang.Class has two useful methods: getName() for getting a string name of the class and getSuperclass() for getting the Class object of the parent class, or null if this is the Object class. For example,

String s = "abc";
Class cls;
cls = s.getClass();  // Represents the String class.
System.out.println(cls.getName()); // Prints "java.lang.String"

Example - What class is the content pane?

I wanted to know the type returned by the JFrame getContentPane() method. The documenation says it returns a Container object, but that only means that it returns Container or a subclass of Container. This program prints the inheritance hierarchy for any object.

// Introspect.java -- Fred Swartz -- 2003.05.04
import java.awt.*;
import javax.swing.*;
import java.util.ArrayList;

class Introspect {

    //====================================================== method main
    public static void main(String[] args) {
        JFrame f = new JFrame();
        
        Container c = f.getContentPane();
        System.out.println("Content pane inheritance hierarchy");
        printAncestors(c);
        
        LayoutManager lm = c.getLayout();
        System.out.println("\n\nLayout for content pane inheritance hierarchy");
        printAncestors(lm);
        
        System.out.println("\n\nDefault JPanel layout inheritance hierarchy");
        printAncestors((new JPanel()).getLayout());
        
    }// end main
    

    //============================================ method printAncestors
    private static void printAncestors(Object obj) {
        ArrayList ancestors = getAncestorList(obj);
        for (int i=ancestors.size()-1; i>=0; i--) {
            Class cls = (Class)ancestors.get(i);
            System.out.println("   " + cls.getName());
        }
    }//end printAncestors
    

    //=========================================== method getAncestorList
    private static ArrayList getAncestorList(Object obj) {
        ArrayList result = new ArrayList();
        result.add(obj.getClass());
        
        for (Class parent=obj.getClass().getSuperclass(); 
                   parent != null; 
                   parent = parent.getSuperclass()) {
            result.add(parent);
        }
       
        return result;
    }//end getAncestorList
    
}//end class

The output from this program is

Content pane inheritance hierarchy
   java.lang.Object
   java.awt.Component
   java.awt.Container
   javax.swing.JComponent
   javax.swing.JPanel


Layout for content pane inheritance hierarchy
   java.lang.Object
   java.awt.BorderLayout
   javax.swing.JRootPane$1

   
Default JPanel layout inheritance hierarchy
   java.lang.Object
   java.awt.FlowLayout