Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: Intermediate
Jan 22, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Obtaining and Using an Object of the Type Class

The java.lang package defines a class called Class. The Class class is different from the class (with a small 'c') keyword used to declare Java classes. The Class class is a Java class that describes other objects. Class does not have any constructors; thus it is not meant to be instantiated in a program. Its purpose in life is to allow a Java program to get information about other Java objects.

An object of type Class can be obtained by calling the getClass() method on a particular Java object. The method getClass() is defined in the Java Object class. You can obtain an object of the type Class for an object "myObject" with the following line of code:

Class myClass = myObject.getClass();
The Class class offers several useful methods that can give you information about the class of myObject, including:
public Constructor[] getConstructors()  // returns class constructors 
public Method[] getMethods()            // returns class methods
public Field[] getFields()              // returns class fields 
public Class getSuperClass()            // returns the superclass
The Class class provides the basis for Java Reflection and Introspection.
Ajit Sagar
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date