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 ww w  .  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.





















Home »
  Java Tutorial »
    java.lang »




Boolean
Byte
Character
Class
Double
Enum
Float
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runnable
Runtime
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable