javawaveblogs-20

Thursday, September 20, 2007

Object Type - Class

In Java we have a class called Class. Type Class is in the package java.lang

What is Class and class? (Please note the second one is with a small 'c')
The one with small 'c' ie., class is a keyword used to declare Java classes.
The one with upper case 'C' is a Type inside java.lang package.
Type Class describes other objects. Class does not have a constructor.
It helps a Java program to get information about other Java objects.
The Class class provides the basis for Java Reflection and Introspection.

If it is not having a constructor, then how to obtain the object of type Class?

The method getClass() is defined in the Java Object class.

An object of type Class can be obtained by calling the getClass() method on a particular Java object.

The following code will give us the object of type Class for Object obj
Class objClass = obj.getClass();
The Class class offers several useful methods like below:
//is used to return class constructors
public Constructor[] getConstructors()

//is used to
return class methods
public Method[] getMethods()

//is used to return class fields
public Field[] getFields()

//is used to return the superclass
public Class getSuperClass()

No comments:

diggthis