Java.lang Package Classes

Java Class - getMethods() Method



The java.lang.Class.getMethods() method returns an array containing Method objects reflecting all the public methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces.

If this Class object represents an interface then the returned array does not contain any implicitly declared methods from Object. Therefore, if no methods are explicitly declared in this interface or any of its superinterfaces then the returned array has length 0. If this Class object represents a primitive type or void, then the returned array has length 0.

Syntax

public Method[] getMethods()
                    throws SecurityException

Parameters

No parameter is required.

Return Value

Returns the array of Method objects representing the public methods of this class.

Exception

Throws SecurityException, if a security manager, s, is present and 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.getMethods() 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.getMethods();
      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)
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
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 java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()

❮ Java.lang - Class