Using the java.lang.Class
java.lang.Class is one of the important classes in java but rarely noticed by java programmers. This class is very useful because it has several methods such as: getClass(), forName() that can be used to load a class, and you can also load Oracle and MySQL drivers. The Class also provides methods such as Class.newInstance() which allows you to create an instance of a class without using the new(). The java.lang.Class has no public constructors, and its instance is created by the JVM when it is loaded. The object of the class Class is also used to represent classes, enumerations, interfaces, and annotations in a running java application. The primitive types byte, short, char, int, float, double, and boolean are represented by instances of java.lang.Class. You can get the instance Class corresponding in this way: int.class, float.class or boolean.class. This class is also used to represent the instance of an array in java. Each painting with the Same type and the same dimension shares the Same Instance of the Class Class. Another use of the java.lang.Class, when implementing the equals() to check if two objects are due Same type or not.The object java.lang.Class is created each time by the JVM that describes the type of the object. All instances of the same class share the same object Class and you can get the object Class by calling the method getClass(). This method is inherited from the java.lang.Object.
Suppose in this example that we have created different instances to compare them to the following:
public static void main(String[] args) {Runtime:
String str1 = new String(), str2 = new String();
System.out.println(str1.getClass().getName());
System.out.println(str2.getClass().getName());
if(str1.getClass().equals(str2.getClass())){
System.out.println("str1 and str2 are instances of the same class");
}else{
System.out.println("str1 and str2 are instances of different classes");
}
Integer A = new Integer(1);
Boolean B = new Boolean(true);
System.out.println(A.getClass().getName());
System.out.println(B.getClass().getName());
if(A.getClass().equals( B.getClass())){
System.out.println("A and B are instances of the same class");
}else{
System.out.println("A and B are instances of different classes");
}
}
java.lang.StringWe need the methods newInstance() and forName() because sometimes we can't know the name of the instantiated class at runtime, so these methods make things simpler for us.
java.lang.String
str1 and str2 are instances of the same class
java.lang.Integer
java.lang.Boolean
A and B are instances of different classes
try {Runtime:
/*newInstance() equivalent to new String()*/
String str2 = str1.getClass().newInstance();
System.out.println(str2.getClass().getName());
System.out.println(str2.getClass().getSimpleName());
System.out.println(str2.getClass().getCanonicalName());
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java.lang.StringThere are other methods to test the type of class:
String
java.lang.String
isEnum(): returns true if the type is an enumeration.
isArray(): returns true if the type is an array.
isInstance(Object o): returns true if a class is an instance of the o.
isLocal(): returns true if the class is declared locally.
String str1 = new String();
String[] array = {"aa","bb","cc"};
String[] array2 = {"fd","be","ce"};
System.out.println(array.getClass().isArray()); true
System.out.println(array.getClass().isInstance(array2)); true
System.out.println(array.getClass().isLocalClass()); false
Get the constructors, interfaces, and methods of a class
Another benefit of using java.lang.Class is that we can display all lists of constructors, attributes, methods, and interfaces using the following methods:java.lang.Class.getConstructors(): Returns an array containing all the Constructor objects related to the public constructors of the class. An array of size 0 is returned if the class has no public constructors, or if the class is an array.
java.lang.Class.getMethods(): returns an array containing all the member methods of the class or interface, including those declared in the class or interface, or those inherited from a parent class and parent interfaces. An array of size 0 is returned if the class has no public method, or if the class is an array.
java.lang.Class.getInterfaces(): This method determines which interfaces are implemented by a class or represented by that object. She Returns an array containing all the interfaces in the class, including those declared in the class or those inherited from a parent class. An array of size 0 is returned if the class has no public interface, or if the class is an array.
Using these three methods is easy, as shown in the following code:
java.lang.Class button = Class.class.forName("javax.swing.JButton");Running this piece of code results in a long list of methods of the java.lang.String, so we'll just show the list of constructors and interfaces:
System.out.println("Constructors of "+button.getClass());
Constructor > [] c = button.getClass().getConstructors();
for(int i=0; i< c.length; i++)
System.out.println(c[i]);
System.out.println("Interfaces of "+button.getClass());
java.lang.Class > [] interfaces = button.getClass().getInterfaces();
for(int i=0; i< interfaces.length; i++)
System.out.println(interfaces[i]);
System.out.println("Methods of "+button.getClass());
Method[] m = button.getClass().getMethods();
for(int i=0; i< m.length; i++)
System.out.println(m[i]);
javax.swing.JButtonReferences:
javax.swing.JButton
interface java.io.Serializable
interface java.lang.reflect.GenericDeclaration
interface java.lang.reflect.Type
interface java.lang.reflect.AnnotatedElement
< br />Class Methods javax.swing.JButton
public static java.lang.Class java.lang.Class.forName(java.lang.String,boolean,java.lang.ClassLoader) throws java.lang.ClassNotFoundException
public static java.lang.Class java.lang.Class.forName(java.lang.String) throws java.lang.ClassNotFoundException
public native boolean java.lang.Class.isAssignableFrom(java.lang.Class)
public native java.lang.Class java.lang.Class.getSuperclass()
public native java.lang.Class java.lang.Class.getComponentType()
public java.lang.String java.lang.Class.toString()
public native boolean java.lang.Class.isInstance(java.lang.Object)
public native int java.lang.Class.getModifiers()
public native boolean java.lang.Class.isInterface()
public native boolean java.lang.Class.isArray()
public native boolean java.lang.Class.isPrimitive()
public java.lang.String java.lang.Class.getName()
public java.lang.Class java.lang.Class.asSubclass(java.lang.Class)
public java.lang.Object java.lang.Class.cast(java.lang.Object)
public java.lang.Class[] java.lang.Class.getClasses()
public java.lang.reflect.Field java.lang.Class.getField(java.lang.String) throws java.lang.NoSuchFieldException,java.lang.SecurityException
public java.lang.reflect.Field[] java.lang.Class.getFields() throws java.lang.SecurityException
public java.lang.reflect.Method java.lang.Class.getMethod(java.lang.String,java.lang.Class[]) throws java.lang.NoSuchMethodException,java.lang.SecurityException
public java.lang.reflect.Method[] java.lang.Class.getMethods() throws java.lang.SecurityException
public java.lang.Package java.lang.Class.getPackage()
public java.net.URL java.lang.Class.getResource(java.lang.String)
public native java.lang.Object[] java.lang.Class.getSigners()
public boolean java.lang.Class.isAnnotation()
public boolean java.lang.Class.isEnum()
public boolean java.lang.Class.isLocalClass()
public boolean java.lang.Class.isSynthetic()
public java.lang.Object java.lang.Class.newInstance() throws java.lang.InstantiationException, java.lang.IllegalAccessException
public boolean java.lang.Class.desiredAssertionStatus()
public java.lang.annotation.Annotation java.lang.Class.getAnnotation(java.lang.Class)
public java.lang.annotation.Annotation[] java.lang.Class.getAnnotations()
public java.lang.String java.lang.Class.getCanonicalName()
public java.lang.ClassLoader java.lang.Class.getClassLoader()
public java.lang.reflect.Constructor java.lang.Class.getConstructor(java.lang.Class[]) throws java.lang.NoSuchMethodException,java.lang.SecurityException
public java.lang.reflect.Constructor[] java.lang.Class.getConstructors() throws java.lang.SecurityException
public java.lang.annotation.Annotation[] java.lang.Class.getDeclaredAnnotations()
public java.lang.Class[] java.lang.Class.getDeclaredClasses() throws java.lang.SecurityException
public java.lang.reflect.Constructor java.lang.Class.getDeclaredConstructor(java.lang.Class[]) throws java.lang.NoSuchMethodException,java.lang.SecurityException
public java.lang.reflect.Constructor[] java.lang.Class.getDeclaredConstructors() throws java.lang.SecurityException
public java.lang.reflect.Field java.lang.Class.getDeclaredField(java.lang.String) throws java.lang.NoSuchFieldException,java.lang.SecurityException
public java.lang.reflect.Field[] java.lang.Class.getDeclaredFields() throws java.lang.SecurityException
public java.lang.reflect.Method java.lang.Class.getDeclaredMethod(java.lang.String,java.lang.Class[]) throws java.lang.NoSuchMethodException,java.lang.SecurityException
public java.lang.reflect.Method[] java.lang.Class.getDeclaredMethods() throws java.lang.SecurityException
public native java.lang.Class java.lang.Class.getDeclaringClass()
public java.lang.Class java.lang.Class.getEnclosingClass()
public java.lang.reflect.Constructor java.lang.Class.getEnclosingConstructor()
public java.lang.reflect.Method java.lang.Class.getEnclosingMethod()
public java.lang.Object[] java.lang.Class.getEnumConstants()
public java.lang.reflect.Type[] java.lang.Class.getGenericInterfaces()
public java.lang.reflect.Type java.lang.Class.getGenericSuperclass()
public native java.lang.Class[] java.lang.Class.getInterfaces()
public java.security.ProtectionDomain java.lang.Class.getProtectionDomain()
public java.io.InputStream java.lang.Class.getResourceAsStream(java.lang.String)
public java.lang.String java.lang.Class.getSimpleName()
public java.lang.reflect.TypeVariable[] java.lang.Class.getTypeParameters()
public boolean java.lang.Class.isAnnotationPresent(java.lang.Class)
public boolean java.lang.Class.isAnonymousClass()
public boolean java.lang.Class.isMemberClass()
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
Javadoc: java.lang.Class