Java Reflection and Classes

How do we handle the class java.lang.Class.

How do we find the Class object of an object.

  • For all existing Java class, there is a corresponding java.lang.Class object.
  • There are three ways to obtain the java.lang.Class object object:
    1. Use the class literal.
      java.lang.Class rectangleClassObject = Rectangle.class;
    2. Call the forName() static method of java.lang.Class.
      java.lang.Class rectangleClassObject = java.lang.Class.forname("Rectangle");
    3. Call the getClass() method of an object
      Rectangle rect = new Rectangle(400,300);
      java.lang.Class rectangleClassObject = rect.getClass();

How do we create an object of a class with the java.lang.Class object.

The newInstance() method

  • The newInstance() method of Class can be used to create new objects. This method requires a no-argument constructor.
    java.lang.Class rectangleClassObject = Rectangle.class;
    Rectangle rectangleObject=(Rectangle)rectangleClassObject.newInstance();
    // In this case the Rectangle class must have a no-argument constructor.
    (the return value from the newInstance() method is an java.lang.Object, so you must cast it to the right one)

How do we fetch general information about a class with the java.lang.Class object.

  • The java.lang.Class object object provides information about the class it belongs to:
    1. The name of the class.


      There is three alternative methods to use:
      // The next method will return the simple name 
      // of the underlying class as given in the source code.
      // (no package information)
      String simpleName = rectangleClassObject.getSimpleName();
      // The next method will return the canonical name 
      // of the underlying class as defined by the Java Language Specification. 
      String simpleName = rectangleClassObject.getCanonicalName();
      // The next method will return the name of the entity  
      // represented by this Class object
      String simpleName = rectangleClassObject.getName();
      // The differences between the two last methods 
      // is when you come to arrays or inner classes.
      // In the most cases they returns the same.
    2. The constructors in a Class.


      There is two alternative methods to use:
      // The next method will return public, protected 
      // and private contructors in the class, Rectangle
      Constructor [] constructors = rectangleClassObject.getConstructors();
      // The next method will return only 
      // public contructors in the class, Rectangle
      Constructor [] constructors = rectangleClassObject.getDeclaredConstructors();
      You will find more about constructors here.
    3. The methods in a Class.


      There is two alternative methods to use:
      // The next method will return only public 
      // methods in the class, Rectangle, and those inherited from superclasses.
      Method [] methods = rectangleClassObject.getMethods();
      // The next method will return public, protected and private 
      // methods in the class, Rectangle, but excludes inherited methods.
      Method [] methods = rectangleClassObject.getDeclaredMethods();
      You will find more about methods here.
    4. The fields in a Class.


      There is two alternative methods to use:
      // The next method will return only public 
      // Fields in the class, Rectangle, and those inherited from superclasses.
      Field [] fields = rectangleClassObject.getFields();
      // The next method will return public, protected and private 
      // Fields in the class, Rectangle, but excludes inherited Fields.
      Field [] fieldss = rectangleClassObject.getDeclaredFields();
      You will find more about fields here.
    5. The annotations in a Class.

      There is two alternative methods to use:
      // The next method will return only public 
      // Annotations in the class, Rectangle, and those inherited from superclasses.
      Annotation [] annotations = rectangleClassObject.getAnnotations();
      // The next method will return public, protected and private 
      // Annotations in the class, Rectangle, but excludes inherited Annotations.
      Annotation [] annotations = rectangleClassObject.getDeclaredAnnotations();
    6. The interfaces in a Class.

      // The next method will return Class objects for 
      // interfaces implemented by the class, Rectangle. 
      // No extended interfaces on those interfaces are included.
      Class [] interfaces = rectangleClassObject.getInterfaces();
      // (If rectangleClassObject was a interface
      // object extended interfaces are included.)
    7. The super-class in a Class.

      Class  superClassObject = rectangleClassObject.getSuperclass();
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.