Java Reflection - Java Class.getDeclaringClass()








Syntax

Class.getDeclaringClass() has the following syntax.

public Class <?> getDeclaringClass()

Example

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

/*from  w  ww.j a  va  2  s  .  c  o  m*/
import java.lang.reflect.Method;

public class Main {

  public static void main(String[] args) {

    Class cls = String.class;

    Method[] m = cls.getMethods();
    for (int i = 0; i < m.length; i++) {
      // returns te declaring class
      Class dec = m[i].getDeclaringClass();
      // displays all methods
      System.out.println("Method = " + m[i].toString());
      System.out.println(" Declaring class: " + dec.toString());
    }
  }
}

The code above generates the following result.