java::lang::Class Class Reference

Inheritance diagram for java::lang::Class:

Inheritance graph
java::lang::Object
[legend]
Collaboration diagram for java::lang::Class:

Collaboration graph
java::lang::Objectjava::lang::ObjectRefjava::lang::Interfacejava::lang::Comparable
[legend]

List of all members.


Detailed Description

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. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.

Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.

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

<blockquote>

     void printClassName(Object obj) {
         System.out.println("The class of " + obj +
                            " is " + obj.getClass().getName());
     }
 
</blockquote>

Author:
unascribed
Version:
1.107, 02/02/00
Since:
JDK1.0

Public Member Functions

virtual String toString () const
 Converts the object to a string.
ObjectRef newInstance () const
 Creates a new instance of the class represented by this Class object.
jboolean isAncestorOf (const Class &src) const
 Determines if the class or interface represented by this Class object is a superclass of the class or interface represented by the specified Class parameter.
jboolean isAssignableFrom (const Class &src) const
 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.
jboolean isInstance (const Object &src) const
 Determines if the specified Object is assignment-compatible with the object represented by this Class.
jboolean isInterface () const
 Determines if the specified Class object represents an interface type.
jboolean isArray () const
 Determines if this Class object represents an array class.
jboolean isPrimitive () const
 Determines if the specified Class object represents a primitive type.
const StringgetName () const
 Returns the fully-qualified name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.
const ClassgetSuperclass () const
 Returns the Class representing the superclass of the entity (class, interface, primitive type or void) represented by this Class.
const Array< jobject > & getInterfaces () const
 Determines the interfaces implemented by the class or interface represented by this object.

Static Public Member Functions

static const ClassforName (const String &className)
 Returns the Class object associated with the class or interface with the given string name.

Friends

class Global
class ClassImpl

Member Function Documentation

virtual String java::lang::Class::toString (  )  const [virtual]

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".

Returns:
a string representation of this class object.

Reimplemented from java::lang::Object.

static const Class& java::lang::Class::forName ( const String className  )  [static]

Returns the Class object associated with the class or interface with the given string name.

Invoking this method is equivalent to:

<blockquote>

  Class.forName(className, true, currentLoader)
 
</blockquote>

where currentLoader denotes the defining class loader of the current class.

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

<blockquote>

   Class t = Class.forName("java.lang.Thread")
 
</blockquote>

A call to forName("X") causes the class named X to be initialized.

Parameters:
className the fully qualified name of the desired class.
Returns:
the Class object for the class with the specified name.
Exceptions:
LinkageError if the linkage fails
ExceptionInInitializerError if the initialization provoked by this method fails
ClassNotFoundException if the class cannot be located

ObjectRef java::lang::Class::newInstance (  )  const

Creates a new instance of the class represented by this Class object.

The class is instantiatied as if by a new expression with an empty argument list. The class is initialized if it has not already been initialized.

If there is a security manager, this method first calls the security manager's checkMemberAccess method with this and Member.PUBLIC as its arguments. If the class is in a package, then this method also calls the security manager's checkPackageAccess method with the package name as its argument. Either of these calls could result in a SecurityException.

Returns:
a newly allocated instance of the class represented by this object.
Exceptions:
IllegalAccessException if the class or initializer is not accessible.
InstantiationException if this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the instantiation fails for some other reason.
ExceptionInInitializerError if the initialization provoked by this method fails.
SecurityException if there is no permission to create a new instance.

jboolean java::lang::Class::isAncestorOf ( const Class src  )  const

Determines if the class or interface represented by this Class object is a superclass 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.

Parameters:
src the Class object to be checked
Returns:
the boolean value indicating whether objects of the type cls is derived from the objects of this class
Exceptions:
NullPointerException if the specified Class parameter is null.
Since:
JDK1.1

jboolean java::lang::Class::isAssignableFrom ( const Class src  )  const

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:
src 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
Exceptions:
NullPointerException if the specified Class parameter is null.
Since:
JDK1.1

jboolean java::lang::Class::isInstance ( const Object src  )  const

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:
src the object to check
Returns:
true if src is an instance of this class
Since:
JDK1.1

jboolean java::lang::Class::isInterface (  )  const

Determines if the specified Class object represents an interface type.

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

jboolean java::lang::Class::isArray (  )  const

Determines if this Class object represents an array class.

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

jboolean java::lang::Class::isPrimitive (  )  const

Determines if the specified Class object represents a primitive type.

There are nine predefined Class objects to represent the eight primitive types and void. These are created by the Java Virtual Machine, and have the same names as the primitive types that they represent, namely boolean, byte, char, short, int, long, float, and double.

These objects may only be accessed via the following public static final variables, and are the only Class objects for which this method returns true.

Returns:
true if and only if this class represents a primitive type
Since:
JDK1.1

const String& java::lang::Class::getName (  )  const

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 "<tt>[</tt>" characters representing the depth of array nesting. Thus:

<blockquote>

 (new Object[3]).getClass().getName()
 
</blockquote>

returns "<code>[Ljava.lang.Object;</code>" and:

<blockquote>

 (new int[3][4][5][6][7][8][9]).getClass().getName()
 
</blockquote>

returns "<code>[[[[[[[I</code>". The encoding of element type names is as follows:

<blockquote>

 B            byte
 C            char
 D            double
 F            float
 I            int
 J            long
 Lclassname;  class or interface
 S            short
 Z            boolean
 
</blockquote>

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.

const Class& java::lang::Class::getSuperclass (  )  const

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.

const Array<jobject>& java::lang::Class::getInterfaces (  )  const

Determines the interfaces implemented by the class or interface represented by this object.

If this object represents a class, the return value is an array containing objects representing all interfaces implemented by the class. The order of the interface objects in the array corresponds to the order of the interface names in the implements clause of the declaration of the class represented by this object. For example, given the declaration: <blockquote>

 class Shimmer implements FloorWax, DessertTopping { ... }
 
</blockquote> suppose the value of s is an instance of Shimmer; the value of the expression: <blockquote>
 s.getClass().getInterfaces()[0]
 
</blockquote> is the Class object that represents interface FloorWax; and the value of: <blockquote>
 s.getClass().getInterfaces()[1]
 
</blockquote> is the Class object that represents interface DessertTopping.

If this object represents an interface, the array contains objects representing all interfaces extended by the interface. The order of the interface objects in the array corresponds to the order of the interface names in the extends clause of the declaration of the interface represented by this object.

If this object represents a class or interface that implements no interfaces, the method returns an array of length 0.

If this object represents a primitive type or void, the method returns an array of length 0.

Returns:
an array of interfaces implemented by this class.


Friends And Related Function Documentation

friend class Global [friend]

friend class ClassImpl [friend]


The documentation for this class was generated from the following file:
Generated on Fri May 16 11:56:35 2008 for CrossPlatformJavaLikeC++API by  doxygen 1.5.3