All About Class Class in Java

0
180
Spread the love
  • 29
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    138
    Shares

Class “Class” in Java

This is a very interesting Class called Class in java. An instance of the Class Class only represents classes and interfaces in the java applications. There are no member elements and public constructors available for Class Class. Class objects are constructed automatically by JVM when Classes are loaded into JVM and by the help of the method defineClass()[it stays in ClassLoader Class]

This class represents a Java class or interface, or any Java type. There is one Class object for each class that is loaded into the Java Virtual Machine, there are special Class objects that represent the Java primitive types.

The TYPE constants defined by Boolean, Integer, and the other primitive “wrapper classes” hold these special Class objects. Array types are also represented by Class objects.

There is no constructor for this class. You can obtain a Class object by calling the getClass() method of any instance of the desired class. A Class object may also be referred to by appending .class to the name of a class.

A class can be dynamically loaded by passing its fully-qualified name (i.e., package name plus class name) to the static Class.forName() method. This method loads the named class, if it is not already loaded, into the Java interpreter, and returns a Class object for it.

The newInstance() method creates an instance of a given class–this allows you to create instances of dynamically loaded classes for which you cannot use the new keyword. Note that this method only works when the target class has a no-argument constructor. See newInstance() in java.lang.reflect.Constructor for a more powerful way to instantiate dynamically loaded classes.

getName() returns the name of the class. getSuperclass() returns its superclass. isInterface() tests whether the Class object represents an interface, and getInterfaces() returns an array of the interfaces that this class implements. The various other get and is methods return other information about the represented class, and form part of the Java Reflection API, along with the classes in java.lang.reflect.

Below is the structure of Class -Class

public final Class java.lang.Class extends java.lang.Object implements java.io.Serializable{
// No Constructor
// Class Methods
public static native Class forName(String className) throws ClassNotFoundException;
// Public Instance Methods
public native ClassLoader getClassLoader();
public Class[] getClasses();
public native Class getComponentType();
public Constructor getConstructor(Class[] parameterTypes) throws NoSuchMethodException, SecurityException;
public Constructor[] getConstructors() throws SecurityException;
public Class[] getDeclaredClasses() throws SecurityException;
public Constructor getDeclaredConstructor(Class[] parameterTypes) throws NoSuchMethodException, SecurityException;
public Constructor[] getDeclaredConstructors() throws SecurityException;
public Field getDeclaredField(String name) throws NoSuchFieldException,SecurityException;
public Field[] getDeclaredFields() throws SecurityException;
public Method getDeclaredMethod(String name, Class[] parameterTypes) throws NoSuchMethodException, SecurityException;
public Method[] getDeclaredMethods() throws SecurityException;
public Class getDeclaringClass();
public Field getField(String name) throws NoSuchFieldException,SecurityException;
public Field[] getFields() throws SecurityException;
public native Class[] getInterfaces();
public Method getMethod(String name, Class[] parameterTypes) throws NoSuchMethodException, SecurityException;
public Method[] getMethods() throws SecurityException;
public native int getModifiers();
public native String getName();
public URL getResource(String name);
public InputStream getResourceAsStream(String name);
public native Object[] getSigners();
public native Class getSuperclass();
public native boolean isArray();
public native boolean isAssignableFrom(Class cls);
public native boolean isInstance(Object obj);
public native boolean isInterface();
public native boolean isPrimitive();
public native Object newInstance() throws InstantiationException, IllegalAccessException;
public String toString(); // Overrides Object
} 
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 29
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    138
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here