Java Reflection - Class.getDeclaredMethods() Examples

Code Snippets Java Reflection Java 


Class:

java.lang.Class

java.lang.Objectjava.lang.Objectjava.lang.Classjava.lang.Classjava.io.SerializableSerializablejava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.TypeTypejava.lang.reflect.AnnotatedElementAnnotatedElementLogicBig

Method:

public Method[] getDeclaredMethods()
                            throws SecurityException
Returns:
the array of Method objects representing all the declared methods of this class


Examples


This example also shows difference between getDeclaredMethods() and getMethods()

package com.logicbig.example.clazz;

import java.lang.reflect.Method;
import java.util.Arrays;

public class GetDeclaredMethodsExample {

public static void main(String... args) {
System.out.println("-- using getLDeclaredMethods() --");
Class<MyClass> c = MyClass.class;
Method[] methods = c.getDeclaredMethods();
Arrays.stream(methods)
.forEach(System.out::println);

System.out.println("-- using getMethods() --");
methods = c.getMethods();
Arrays.stream(methods)
.forEach(System.out::println);
}

private static class MyClass {
private void aMethod() { }

public void anotherMethod(int i) { }
}
}

Output

-- using getLDeclaredMethods() --
private void com.logicbig.example.clazz.GetDeclaredMethodsExample$MyClass.aMethod()
public void com.logicbig.example.clazz.GetDeclaredMethodsExample$MyClass.anotherMethod(int)
-- using getMethods() --
public void com.logicbig.example.clazz.GetDeclaredMethodsExample$MyClass.anotherMethod(int)
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()




See Also