Java Reflection - Java Class.getComponentType()








Syntax

Class.getComponentType() has the following syntax.

public Class <?> getComponentType()

Example

In the following code shows how to use Class.getComponentType() method.

/* w  ww . ja v a  2 s.  c o m*/
public class Main {

  public static void main(String[] args) {

    String[] arr = new String[] { "java2s.com" };

    // returns the Class representing the component type
    Class arrClass = arr.getClass();
    Class componentType = arrClass.getComponentType();
    if (componentType != null) {
      System.out.println("ComponentType = " + componentType.getName());
    } else {
      System.out.println("ComponentType is null");
    }
  }
}

The code above generates the following result.