java.lang
Class Class<T>

java.lang.Object
  extended by java.lang.Class<T>
Type Parameters:
T - the type of the class modeled by this Class object. For example, the type of String.class is Class<String>. Use Class<?> if the class being modeled is unknown.

@TransactionType(value=NOT_SUPPORTED)
public final class Class<T>
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());
 }
 

Since:
JDK1.0, CLDC 1.0, Java Card 3.0

Method Summary
 boolean desiredAssertionStatus()
          Returns the assertion status that would be assigned to this class if it were to be initialized at the time this method is invoked.
static Class<?> forName(String className)
          Returns the Class object associated with the class with the given string name.
 String getName()
          Returns the fully-qualified name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.
 InputStream getResourceAsStream(String name)
          Finds a resource with a given name from the search path used to load this Class instance.
 Class<? super T> getSuperclass()
          Returns the Class representing the superclass of the entity (class, interface, primitive type or void) represented by this Class.
 boolean isAnnotation()
          Returns true if this Class object represents an annotation type.
 boolean isArray()
          Determines if this Class object represents an array class.
 boolean isAssignableFrom(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.
 boolean isEnum()
          Returns true if and only if this class was declared as an enum in the source code.
 boolean isInstance(Object obj)
          Determines if the specified Object is assignment-compatible with the object represented by this Class.
 boolean isInterface()
          Determines if the specified Class object represents an interface type.
 T newInstance()
          Creates a new instance of a class.
 String toString()
          Converts the object to a string.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

toString

public String toString()
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".

Overrides:
toString in class Object
Returns:
a string representation of this class object.

forName

public static Class<?> forName(String className)
                        throws ClassNotFoundException
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.

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

Parameters:
className - the fully qualified name of the desired class.
Returns:
the Class object for the class with the specified name.
Throws:
ClassNotFoundException - if the class could not be found.
SecurityException - if the permission to dynamically load the class is not granted.
Error - if the function fails for any other reason.
Since:
JDK1.0, Java Card 3.0

newInstance

public T newInstance()
              throws InstantiationException,
                     IllegalAccessException
Creates a new instance of a class.

Returns:
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.
Throws:
IllegalAccessException - if the class or initializer is not accessible.
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

isInstance

public boolean isInstance(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.

Parameters:
obj - the object to check
Returns:
true if obj is an instance of this class
Since:
JDK1.1

isAssignableFrom

public boolean isAssignableFrom(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.

Parameters:
cls - the Class object to be checked
Returns:
the boolean value indicating whether objects of the type cls can be assigned to objects of this class
Throws:
NullPointerException - if the specified Class parameter is null.
Since:
JDK1.1

isInterface

public boolean isInterface()
Determines if the specified Class object represents an interface type.

Returns:
true if this object represents an interface; false otherwise.

isAnnotation

public boolean isAnnotation()
Returns true if this Class object represents an annotation type. Note that if this method returns true, isInterface() would also return true, as all annotation types are also interfaces.

Returns:
true if this class object represents an annotation type; false otherwise
Since:
JDK1.5, Java Card 3.0

isEnum

public boolean isEnum()
Returns true if and only if this class was declared as an enum in the source code.

Returns:
true if and only if this class was declared as an enum in the source code
Since:
JDK1.5, Java Card 3.0

isArray

public boolean isArray()
Determines if this Class object represents an array class.

Returns:
true if this object represents an array class; false otherwise.
Since:
JDK1.1

getName

public String getName()
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
  I            int
  J            long
  L<i>classname;</i>  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.

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

getResourceAsStream

public InputStream getResourceAsStream(String name)
Finds a resource with a given name from the search path used to load this Class instance. This method returns null if no resource with this name is found.

The resource names can be represented in two different formats: absolute or relative.

Absolute format:

Relative format:

In the absolute format, the programmer provides a fully qualified name that includes both the full path and the name of the resource inside the JAR file. In the path names, the character "/" is used as the separator.

In the relative format, the programmer provides only the name of the actual resource. Relative names are converted to absolute names by the system by prepending the resource name with the fully qualified package name of class upon which the getResourceAsStream method was called.

The rules for searching resources associated with a given class are described in chapter 6 of Runtime Environment Specification for the Java Card Platform, Connected Edition.

Parameters:
name - name of the desired resource
Returns:
a java.io.InputStream object.
Throws:
NullPointerException - if the specified name parameter is null.

desiredAssertionStatus

public boolean desiredAssertionStatus()
Returns the assertion status that would be assigned to this class if it were to be initialized at the time this method is invoked.
The enabling control of assertions is implementation dependent.

Returns:
the desired assertion status of the specified class.
Since:
JDK1.4, Java Card 3.0

getSuperclass

public Class<? super T> getSuperclass()
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.

Returns:
the superclass of the class represented by this object.


Copyright (c) 2009 Sun Microsystems, Inc. All rights reserved.