Home »
Java programming language
Java Class class getMethods() method with example
Class class getMethods() method: Here, we are going to learn about the getMethods() method of Class class with its syntax and example.
Submitted by Preeti Jain, on November 16, 2019
Class class getMethods() method
- getMethods() method is available in java.lang package.
- getMethods() method is used to return an array of Method objects that indicate all the public methods of the class or interfaces along with it includes all inherited public methods from parent classes or parent interfaces.
- getMethods() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- getMethods() method may throw an exception at the time of returning an array of Method objects.
SecurityException: In this exception, it may raise when the security manager exists.
Syntax:
public Method[] getMethods();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of this method is Method[], it returns an array of Method object denoting all the public method of this class including all public inherited Methods represented by this Class object.
Note:
- When class or interface does not contain public method, 0 is returned.
- When this class object holds primitive, an array class, or void type, 0 is returned.
Example:
// Java program to demonstrate the example
// of Method[] getMethods ()
// method of Class
import java.lang.reflect.*;
public class GetMethodsOfClass {
public static void main(String[] args) throws Exception {
Thread th = new Thread();
// Get Class object of String
Class cl = th.getClass();
// Calling Thread Class Methods
Method[] methods = cl.getMethods();
for (int i = 0; i < methods.length; ++i) {
System.out.print("String Class Methods = ");
System.out.println(methods[i].toString());
}
}
}
Output
String Class Methods = public void java.lang.Thread.run()
String Class Methods = public java.lang.String java.lang.Thread.toString()
String Class Methods = public boolean java.lang.Thread.isInterrupted()
String Class Methods = public static native java.lang.Thread java.lang.Thread.currentThread()
String Class Methods = public static void java.lang.Thread.onSpinWait()
String Class Methods = public final java.lang.String java.lang.Thread.getName()
String Class Methods = public final void java.lang.Thread.join() throws java.lang.InterruptedException
String Class Methods = public final synchronized void java.lang.Thread.join(long,int) throws java.lang.InterruptedException
String Class Methods = public final synchronized void java.lang.Thread.join(long) throws java.lang.InterruptedException
String Class Methods = public final java.lang.ThreadGroup java.lang.Thread.getThreadGroup()
String Class Methods = public void java.lang.Thread.setContextClassLoader(java.lang.ClassLoader)
String Class Methods = public java.lang.StackTraceElement[] java.lang.Thread.getStackTrace()
String Class Methods = public static native boolean java.lang.Thread.holdsLock(java.lang.Object)
String Class Methods = public final void java.lang.Thread.checkAccess()
String Class Methods = public static void java.lang.Thread.dumpStack()
String Class Methods = public final void java.lang.Thread.setPriority(int)
String Class Methods = public final void java.lang.Thread.setDaemon(boolean)
String Class Methods = public synchronized void java.lang.Thread.start()
String Class Methods = public static native void java.lang.Thread.yield()
String Class Methods = public static void java.lang.Thread.sleep(long,int) throws java.lang.InterruptedException
String Class Methods = public static native void java.lang.Thread.sleep(long) throws java.lang.InterruptedException
String Class Methods = public final void java.lang.Thread.stop()
String Class Methods = public void java.lang.Thread.interrupt()
String Class Methods = public static boolean java.lang.Thread.interrupted()
String Class Methods = public final native boolean java.lang.Thread.isAlive()
String Class Methods = public final void java.lang.Thread.suspend()
String Class Methods = public final void java.lang.Thread.resume()
String Class Methods = public final int java.lang.Thread.getPriority()
String Class Methods = public final synchronized void java.lang.Thread.setName(java.lang.String)
String Class Methods = public static int java.lang.Thread.activeCount()
String Class Methods = public static int java.lang.Thread.enumerate(java.lang.Thread[])
String Class Methods = public native int java.lang.Thread.countStackFrames()
String Class Methods = public final boolean java.lang.Thread.isDaemon()
String Class Methods = public java.lang.ClassLoader java.lang.Thread.getContextClassLoader()
String Class Methods = public static java.util.Map java.lang.Thread.getAllStackTraces()
String Class Methods = public long java.lang.Thread.getId()
String Class Methods = public java.lang.Thread$State java.lang.Thread.getState()
String Class Methods = public static void java.lang.Thread.setDefaultUncaughtExceptionHandler(java.lang.Thread$UncaughtExceptionHandler)
String Class Methods = public static java.lang.Thread$UncaughtExceptionHandler java.lang.Thread.getDefaultUncaughtExceptionHandler()
String Class Methods = public java.lang.Thread$UncaughtExceptionHandler java.lang.Thread.getUncaughtExceptionHandler()
String Class Methods = public void java.lang.Thread.setUncaughtExceptionHandler(java.lang.Thread$UncaughtExceptionHandler)
String Class Methods = public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
String Class Methods = public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
String Class Methods = public final void java.lang.Object.wait() throws java.lang.InterruptedException
String Class Methods = public boolean java.lang.Object.equals(java.lang.Object)
String Class Methods = public native int java.lang.Object.hashCode()
String Class Methods = public final native java.lang.Class java.lang.Object.getClass()
String Class Methods = public final native void java.lang.Object.notify()
String Class Methods = public final native void java.lang.Object.notifyAll()