Monday 11 March 2013

Class Declaration


Class
Declaration

public final class Class
java.lang.Object
|
+--java.lang.Class
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.
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());
}


Methods
static Class forName(String className)
String getName()
java.io.InputStream getResourceAsStream(String name)
boolean isArray()
boolean isAssignableFrom(Class cls)
boolean isInstance(Object obj)
boolean isInterface()
Object newInstance()
String toString()


Methods inherited from class Object
equals(Object), getClass(), hashCode(), notify(), notifyAll(), wait(), wait(), wait()


Methods
toString()
Declaration:
public java.lang.String toString()
Description:
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(String)
Declaration:
public static java.lang.Class forName(java.lang.String className)
throws ClassNotFoundException
Description:
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: Class t = Class.forName(“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.
Error - if the function fails for any other reason.




No comments:

Post a Comment