FileDocCategorySizeDatePackage
Class.javaAPI DocphoneME MR2 API (J2ME)15029Wed May 02 17:59:54 BST 2007java.lang

Class

public final class Class extends Object
Instances of the class Class represent classes and interfaces in a running Java application. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions.

Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded.

The following example uses a Class object to print the class name of an object:

void printClassName(Object obj) {
System.out.println("The class of " + obj +
" is " + obj.getClass().getName());
}
version
1.106, 12/04/99 (CLDC 1.0, Spring 2000)
since
JDK1.0

Fields Summary
private transient Object
vmClass
private int
status
private Thread
thread
private static final int
IN_PROGRESS
private static final int
VERIFIED
private static final int
INITIALIZED
private static final int
ERROR
Constructors Summary
private Class()

Methods Summary
public static native java.lang.ClassforName(java.lang.String className)
Returns the Class object associated with the class with the given string name. Given the fully-qualified name for a class or interface, this method attempts to locate, load and link the class. If it succeeds, returns the Class object representing the class. If it fails, the method throws a ClassNotFoundException.

For example, the following code fragment returns the runtime Class descriptor for the class named java.lang.Thread:

    Class t = Class.forName("java.lang.Thread")

param
className the fully qualified name of the desired class.
return
the Class descriptor for the class with the specified name.
exception
ClassNotFoundException if the class could not be found.
since
JDK1.0

public native java.lang.StringgetName()
Returns the fully-qualified name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.

If this Class object represents a class of arrays, then the internal form of the name consists of the name of the element type in Java signature format, preceded by one or more "[" characters representing the depth of array nesting. Thus:

(new Object[3]).getClass().getName()
returns "[Ljava.lang.Object;" and:
(new int[3][4][5][6][7][8][9]).getClass().getName()
returns "[[[[[[[I". The encoding of element type names is as follows:
B byte
C char
D double
F float
I int
J long
Lclassname; class or interface
S short
Z boolean
The class or interface name classname is given in fully qualified form as shown in the example above.

return
the fully qualified name of the class or interface represented by this object.

public java.io.InputStreamgetResourceAsStream(java.lang.String name)
Finds a resource with a given name. This method returns null if no resource with this name is found. The rules for searching resources associated with a given class are profile specific.

param
name name of the desired resource
return
a java.io.InputStream object.
since
JDK1.1

        try {
            if (name.length() > 0 && name.charAt(0) == '/") {
                name = name.substring(1);
            } else {
                String className = this.getName();
                int dotIndex = className.lastIndexOf('.");
                if (dotIndex >= 0) {
                    name = className.substring(0, dotIndex + 1).replace('.", '/") 
                           + name;
                }
            }
            return new com.sun.cldc.io.ResourceInputStream(name);
        } catch (java.io.IOException x) {
            return null;
        }
    
private native java.lang.ClassgetSuperclass()
Returns the Class representing the superclass of the entity (class, interface, primitive type or void) represented by this Class. If this Class represents either the Object class, an interface, a primitive type, or void, then null is returned. If this object represents an array class then the Class object representing the Object class is returned. Note that this method is not supported by CLDC. We have made the method private, since it is needed by our implementation.

return
the superclass of the class represented by this object.

private native voidinit9()
Initialization at step 9: If ENABLE_ISOLATES == false Remove the method after the class is initialized. If ENABLE_ISOLATES == true, clear class initialization barrier.

voidinitialize()


    // Native for invoking <clinit>
       

                                  
       

       

    /*
     * Implements the 11 step program detailed in Java Language Specification
     * 12.4.2
     */
        
      // Step 1
      synchronized (this) {
        //  Step 2
        while ((status & IN_PROGRESS) != 0 && thread != Thread.currentThread()) {
          try{ 
            wait();
          } catch (InterruptedException e) {
          }
        }

        // Step 3
        if ((status & IN_PROGRESS) != 0 && thread == Thread.currentThread()) {
          return;
        }

        // Step 4
        if ((status & INITIALIZED) != 0) {
          return;
        }

        // Step 5
        if (status == ERROR) {
          throw new Error("NoClassDefFoundError:"+getName());
        }
        /* Note: CLDC 1.0 does not have NoClassDefFoundError class */

        // Step 6
        status |= IN_PROGRESS;
        thread = Thread.currentThread();
      }

      try {
        // Step 7
        invoke_verify();
        Class s = getSuperclass();
        if (s != null && (s.status & INITIALIZED) == 0) { 
          // The test of s.status is not part of the spec, but it saves us
          // doing a lot of work in the most common case.
          s.initialize();
        }

        // Step 8
        invoke_clinit();

        // Step 9
        synchronized (this) {
          status &= ~IN_PROGRESS;
          status |= INITIALIZED;
          thread = null;
          init9();
          notifyAll();
        }
      } catch(Throwable e) {
          // Step 10 and 11
          // CR 6224346, The cldc_vm threading mechanism is such that
          // we can just jam these values in without fear of another
          // thread doing the same since only this thread can be
          // executing the initialize() method and the scheduler is
          // non-preemptive.  We do this here in case the monitorenter
          // fails due to OOME because some other thread holds the lock,
          // memory is low and we need to allocate a ConditionDesc to
          // wait for the lock.
        status = ERROR;
        thread = null;
        synchronized (this) {
          notifyAll();
          throwError(e);
        }
      }
    
private native voidinvoke_clinit()

private native voidinvoke_verify()

public native booleanisArray()
Determines if this Class object represents an array class.

return
true if this object represents an array class; false otherwise.
since
JDK1.1

public native booleanisAssignableFrom(java.lang.Class cls)
Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter. It returns true if so; otherwise it returns false. If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false.

Specifically, this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion. See The Java Language Specification, sections 5.1.1 and 5.1.4 , for details.

param
cls the Class object to be checked
return
the boolean value indicating whether objects of the type cls can be assigned to objects of this class
exception
NullPointerException if the specified Class parameter is null.
since
JDK1.1

public native booleanisInstance(java.lang.Object obj)
Determines if the specified Object is assignment-compatible with the object represented by this Class. This method is the dynamic equivalent of the Java language instanceof operator. The method returns true if the specified Object argument is non-null and can be cast to the reference type represented by this Class object without raising a ClassCastException. It returns false otherwise.

Specifically, if this Class object represents a declared class, this method returns true if the specified Object argument is an instance of the represented class (or of any of its subclasses); it returns false otherwise. If this Class object represents an array class, this method returns true if the specified Object argument can be converted to an object of the array class by an identity conversion or by a widening reference conversion; it returns false otherwise. If this Class object represents an interface, this method returns true if the class or any superclass of the specified Object argument implements this interface; it returns false otherwise. If this Class object represents a primitive type, this method returns false.

param
obj the object to check
return
true if obj is an instance of this class
since
JDK1.1

public native booleanisInterface()
Determines if the specified Class object represents an interface type.

return
true if this object represents an interface; false otherwise.

public native java.lang.ObjectnewInstance()
Creates a new instance of a class.

return
a newly allocated instance of the class represented by this object. This is done exactly as if by a new expression with an empty argument list.
exception
IllegalAccessException if the class or initializer is not accessible.
exception
InstantiationException if an application tries to instantiate an abstract class or an interface, or if the instantiation fails for some other reason.
since
JDK1.0

private java.lang.ErrorthrowError(java.lang.Throwable e)

 
        throw (e instanceof Error) ? (Error)e 
            : new Error("Static initializer: " + e.getClass().getName() +
                        ", " + e.getMessage());
    
public java.lang.StringtoString()
Converts the object to a string. The string representation is the string "class" or "interface", followed by a space, and then by the fully qualified name of the class in the format returned by getName. If this Class object represents a primitive type, this method returns the name of the primitive type. If this Class object represents void this method returns "void".

return
a string representation of this class object.

        return (isInterface() ? "interface " :  "class ") + getName();