Java Class - getDeclaredMethods() Method
The java.lang.Class.getDeclaredMethods() method returns an array containing Method objects reflecting all the declared methods of the class or interface represented by this Class object, including public, protected, default (package) access, and private methods, but excluding inherited methods.
If this Class object represents a class or interface with no declared methods, then the returned array has length 0. If this Class object represents an array type, a primitive type, or void, then the returned array has length 0.
Syntax
public Method[] getDeclaredMethods() throws SecurityException
Parameters
No parameter is required.
Return Value
Returns the array of Method objects representing all the declared methods of this class.
Exception
Throws SecurityException, if a security manager, s, is present and any of the following conditions is met:
- the caller's class loader is not the same as the class loader of this class and invocation of s.checkPermission method with RuntimePermission("accessDeclaredMembers") denies access to the declared methods within this class.
- the caller's class loader is not the same as or an ancestor of the class loader for the current class and invocation of s.checkPackageAccess() denies access to the package of this class.
Example:
The example below shows usage of the java.lang.Class.getDeclaredMethods() method.
import java.lang.*; import java.lang.reflect.*; public class MyClass { public static void main(String[] args) { try { Class cls = Class.forName("java.lang.Boolean"); //print accessible public methods Method m[] = cls.getDeclaredMethods(); System.out.println("Methods are: "); for(Method i: m) System.out.println(i); } catch (Exception e) { System.out.println(e); } } }
The output of the above code will be:
Methods are: public boolean java.lang.Boolean.equals(java.lang.Object) public java.lang.String java.lang.Boolean.toString() public static java.lang.String java.lang.Boolean.toString(boolean) public static int java.lang.Boolean.hashCode(boolean) public int java.lang.Boolean.hashCode() public int java.lang.Boolean.compareTo(java.lang.Boolean) public int java.lang.Boolean.compareTo(java.lang.Object) public static boolean java.lang.Boolean.getBoolean(java.lang.String) public boolean java.lang.Boolean.booleanValue() public static java.lang.Boolean java.lang.Boolean.valueOf(java.lang.String) public static java.lang.Boolean java.lang.Boolean.valueOf(boolean) public static int java.lang.Boolean.compare(boolean,boolean) public static boolean java.lang.Boolean.parseBoolean(java.lang.String) public static boolean java.lang.Boolean.logicalAnd(boolean,boolean) public static boolean java.lang.Boolean.logicalOr(boolean,boolean) public static boolean java.lang.Boolean.logicalXor(boolean,boolean)
❮ Java.lang - Class