This documentation is archived and is not being maintained.

java.lang.Class

Visual Studio 2005

Represents class types and interface types. Using this class, you can determine the constructors, methods, fields, and other information of a type at runtime.


public final class Class implements Serializable

// class_overview.jsl

import java.lang.reflect.*;

public class Program
{
    public static void main(String[] args)
    {
        // Get the Class object associated with
        // "java.lang.String".
        try
        {
            Class stringClass = Class.forName("java.lang.String");
            System.out.println("Class found: " +
                stringClass.getName());

            // Get the Class object associated with "Program".
            Class thisClass = Class.forName("Program");

            // Get the name of this class.
            String name = thisClass.getName();
            System.out.println("\nThis class name: " + name);

            // Get all the public Class objects associated with the
            // Program class.
            Class[] classes = thisClass.getClasses();
            System.out.println();
            for (int i = 0; i < classes.length; i++)
            {
                System.out.println("Class found: " +
                    classes[i].getName());
            }

            // Get all the Class objects associated with the
            // Program class.
            classes = thisClass.getDeclaredClasses();
            System.out.println();
            for (int i = 0; i < classes.length; i++)
            {
                System.out.println("Class found: " +
                    classes[i].getName());
            }

            // Get all the public constructors for ths class.
            Constructor[] ctors = thisClass.getConstructors();
            System.out.println();
            for (int i = 0; i < ctors.length; i++)
            {
                System.out.println("Public constructor found: " +
                    ctors[i].toString());
            }

            // Get all the constructors for ths class.
            ctors = thisClass.getDeclaredConstructors();
            System.out.println();
            for (int i = 0; i < ctors.length; i++)
            {
                System.out.println("Constructor found: " +
                    ctors[i].toString());
            }

            // Get all the public fields associated with this class.
            Field[] fields = thisClass.getFields();
            System.out.println();
            for (int i = 0; i < fields.length; i++)
            {
                System.out.println("Public field found: " +
                    fields[i].toString());
            }

            // Get all the fields associated with this class.
            fields = thisClass.getDeclaredFields();
            System.out.println();
            for (int i = 0; i < fields.length; i++)
            {
                System.out.println("Field found: " +
                    fields[i].toString());
            }

            // Get the public methods associated with this class.
            Method[] methods = thisClass.getMethods();
            System.out.println();
            for (int i = 0; i < methods.length; i++)
            {
                System.out.println("Public method found: " +
                    methods[i].toString());
            }

            // Get all the methods associated with this class.
            methods = thisClass.getDeclaredMethods();
            System.out.println();
            for (int i = 0; i < methods.length; i++)
            {
                System.out.println("Method found: " +
                    methods[i].toString());
            }

            // Get the interfaces associated with this class.
            Class[] interfaces = thisClass.getInterfaces();
            System.out.println();
            for (int i = 0; i < interfaces.length; i++)
            {
                System.out.println("Interface found: " +
                    interfaces[i].toString());
            }

            // Get the modifiers for this class.
            int i = thisClass.getModifiers();
            String mods = Modifier.toString(i);
            System.out.println("\nClass modifiers: " + mods);

            // Get the super class of this class.
            Class superClass = thisClass.getSuperclass();
            System.out.println("\nSuper class name: " +
                superClass.getName());
        }
        catch (ClassNotFoundException ex)
        {
            System.out.println(ex.toString());
        }
    }

    public class InnerPublicClass
    {
        public InnerPublicClass()
        {
        }
    }

    private class InnerPrivateClass
    {
        public InnerPrivateClass()
        {
        }
    }
}

/*
Output:
Class found: java.lang.String

This class name: Program

Class found: Program$InnerPublicClass

Class found: Program$InnerPublicClass
Class found: Program$InnerPrivateClass

Public constructor found: public Program()

Constructor found: public Program()



Public method found: public final java.lang.Class java.lang.Object.getClass()
Public method found: public final void java.lang.Object.notify()
Public method found: public final void java.lang.Object.notifyAll()
Public method found: public final void java.lang.Object.wait() throws java.lang.InterruptedException
Public method found: public final void java.lang.Object.wait(long) throws java.lang.InterruptedException
Public method found: public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
Public method found: public static void Program.main(java.lang.String[])
Public method found: public java.lang.String java.lang.Object.toString()
Public method found: public boolean java.lang.Object.equals(java.lang.Object)
Public method found: public int java.lang.Object.hashCode()

Method found: public static void Program.main(java.lang.String[])


Class modifiers: public synchronized

Super class name: java.lang.Object
*/
Show: