java.lang
Class Class

java.lang.Object
  extended by java.lang.Class

public final class Class
extends Object

A Class represents a Java type. There will never be multiple Class objects with identical names and ClassLoaders. Primitive types, array types, and void also have a Class object.

Arrays with identical type and number of dimensions share the same class. The array class ClassLoader is the same as the ClassLoader of the element type of the array (which can be null to indicate the bootstrap classloader). The name of an array class is [<signature format>;.

For example, String[]'s class is [Ljava.lang.String;. boolean, byte, short, char, int, long, float and double have the "type name" of Z,B,S,C,I,J,F,D for the purposes of array classes. If it's a multidimensioned array, the same principle applies: int[][][] == [[[I.

There is no public constructor - Class objects are obtained only through the virtual machine, as defined in ClassLoaders.

Since:
1.0
See Also:
ClassLoader

Method Summary
static Class forName(String className)
          Use the classloader of the current class to load, link, and initialize a class.
 String getName()
          Get the name of this class, separated by dots for package separators.
 InputStream getResourceAsStream(String resourceName)
          Get a resource using this class's package using the getClassLoader().getResourceAsStream() method.
 Object newInstance()
          Get a new instance of this class by calling the no-argument constructor.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

forName

public static Class forName(String className)
                     throws ClassNotFoundException
Use the classloader of the current class to load, link, and initialize a class. This is equivalent to your code calling Class.forName(name, true, getClass().getClassLoader()).

Parameters:
name - the name of the class to find
Returns:
the Class object representing the class
Throws:
ClassNotFoundException - if the class was not found by the classloader
LinkageError - if linking the class fails
ExceptionInInitializerError - if the class loads, but an exception occurs during initialization

getName

public String getName()
Get the name of this class, separated by dots for package separators. If the class represents a primitive type, or void, then the name of the type as it appears in the Java programming language is returned. For instance, Byte.TYPE.getName() returns "byte". Arrays are specially encoded as shown on this table.
 array type          [element type
                     (note that the element type is encoded per
                      this table)
 boolean             Z
 byte                B
 char                C
 short               S
 int                 I
 long                J
 float               F
 double              D
 void                V
 class or interface, alone: <dotted name>
 class or interface, as element type: L<dotted name>;
 

Returns:
the name of this class

getResourceAsStream

public InputStream getResourceAsStream(String resourceName)
Get a resource using this class's package using the getClassLoader().getResourceAsStream() method. If this class was loaded using the system classloader, ClassLoader.getSystemResource() is used instead.

If the name you supply is absolute (it starts with a /), then the leading / is removed and it is passed on to getResource(). If it is relative, the package name is prepended, and .'s are replaced with /.

The URL returned is system- and classloader-dependent, and could change across implementations.

Parameters:
resourceName - the name of the resource, generally a path
Returns:
an InputStream with the contents of the resource in it, or null
Throws:
NullPointerException - if name is null
Since:
1.1

newInstance

public Object newInstance()
                   throws InstantiationException,
                          IllegalAccessException
Get a new instance of this class by calling the no-argument constructor. The class is initialized if it has not been already. A security check may be performed, with checkMemberAccess(this, Member.PUBLIC) as well as checkPackageAccess both having to succeed.

Returns:
a new instance of this class
Throws:
InstantiationException - if there is not a no-arg constructor for this class, including interfaces, abstract classes, arrays, primitive types, and void; or if an exception occurred during the constructor
IllegalAccessException - if you are not allowed to access the no-arg constructor because of scoping reasons
java.lang.SecurityException - if the security check fails
ExceptionInInitializerError - if class initialization caused by this call fails with an exception