Java Class getMethods() Method



Description

The Java Class getMethods() method returns an array containing Method objects reflecting all the public member 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.

The method returns an array of length 0 if this Class object represents a class or interface that has no public member methods, or if this Class object represents a primitive type or void.

Declaration

Following is the declaration for java.lang.Class.getMethods() method

public Method[] getMethods() throws SecurityException

Parameters

NA

Return Value

This method returns the array of Method objects representing the public methods of this class.

Exception

SecurityException − If a security manager, s, is present.

Getting Methods of a Class Example

The following example shows the usage of java.lang.Class.getMethods() method. In this program, we've created an instance of ClassDemo and then using getClass() method, the class of the instance is retrieved. Now using getMethods(), we've retrieved all methods and printed them.

package com.tutorialspoint;

import java.lang.reflect.Method;

public class ClassDemo {

   public static void main(String[] args) {

      try {
         ClassDemo cls = new ClassDemo();
         Class c = cls.getClass();
         System.out.println("Methods =");
      
         /* returns the array of Method objects representing the public 
            methods of this class */
         Method m[] = c.getMethods();
         for(int i = 0; i < m.length; i++) {
            System.out.println(m[i]);
         }
      } catch (Exception e) {
         System.out.println("Exception: " + e);
      }
   }
   public Integer show() {
      return 1;
   }
    
   public void showLong(Long l) {
      this.l = l;
   }
   long l = 78655;
} 

Output

Let us compile and run the above program, this will produce the following result −

Methods =
public static void com.tutorialspoint.ClassDemo.main(java.lang.String[])
public java.lang.Integer com.tutorialspoint.ClassDemo.show()
public void com.tutorialspoint.ClassDemo.showLong(java.lang.Long)
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()
public final 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

Getting Methods of a Thread Example

The following example shows the usage of java.lang.Class.getMethods() method. In this program, we've retrieved class of Thread. Now using getMethods(), we've retrieved all methods and printed them.

package com.tutorialspoint;

import java.lang.reflect.Method;

public class ClassDemo {

   public static void main(String[] args) {

      try {
         Class cls = Thread.class;
         System.out.println("Methods =");
      
         /* returns the array of Method objects representing the public 
            methods of this class */
         Method m[] = cls.getMethods();
         for(int i = 0; i < m.length; i++) {
            System.out.println(m[i]);
         }
      } catch (Exception e) {
         System.out.println("Exception: " + e);
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Methods =
public final java.lang.String java.lang.Thread.getName()
public java.lang.StackTraceElement[] java.lang.Thread.getStackTrace()
public void java.lang.Thread.run()
public void java.lang.Thread.interrupt()
public java.lang.String java.lang.Thread.toString()
public static native java.lang.Thread java.lang.Thread.currentThread()
public static void java.lang.Thread.onSpinWait()
public final boolean java.lang.Thread.isVirtual()
public final void java.lang.Thread.join(long) throws java.lang.InterruptedException
public final void java.lang.Thread.join(long,int) throws java.lang.InterruptedException
public final void java.lang.Thread.join() throws java.lang.InterruptedException
public final boolean java.lang.Thread.join(java.time.Duration) throws java.lang.InterruptedException
public void java.lang.Thread.setContextClassLoader(java.lang.ClassLoader)
public final java.lang.ThreadGroup java.lang.Thread.getThreadGroup()
public final void java.lang.Thread.checkAccess()
public static void java.lang.Thread.dumpStack()
public final void java.lang.Thread.setPriority(int)
public final void java.lang.Thread.setDaemon(boolean)
public void java.lang.Thread.start()
public java.lang.ClassLoader java.lang.Thread.getContextClassLoader()
public final int java.lang.Thread.getPriority()
public final boolean java.lang.Thread.isDaemon()
public static boolean java.lang.Thread.interrupted()
public static int java.lang.Thread.activeCount()
public static int java.lang.Thread.enumerate(java.lang.Thread[])
public final boolean java.lang.Thread.isAlive()
public final long java.lang.Thread.threadId()
public static void java.lang.Thread.setDefaultUncaughtExceptionHandler(java.lang.Thread$UncaughtExceptionHandler)
public java.lang.Thread$UncaughtExceptionHandler java.lang.Thread.getUncaughtExceptionHandler()
public static void java.lang.Thread.yield()
public static void java.lang.Thread.sleep(long,int) throws java.lang.InterruptedException
public static void java.lang.Thread.sleep(java.time.Duration) throws java.lang.InterruptedException
public static void java.lang.Thread.sleep(long) throws java.lang.InterruptedException
public static java.lang.Thread$Builder$OfPlatform java.lang.Thread.ofPlatform()
public static java.lang.Thread$Builder$OfVirtual java.lang.Thread.ofVirtual()
public static java.lang.Thread java.lang.Thread.startVirtualThread(java.lang.Runnable)
public final void java.lang.Thread.stop()
public boolean java.lang.Thread.isInterrupted()
public final void java.lang.Thread.suspend()
public final void java.lang.Thread.resume()
public final synchronized void java.lang.Thread.setName(java.lang.String)
public int java.lang.Thread.countStackFrames()
public static native boolean java.lang.Thread.holdsLock(java.lang.Object)
public static java.util.Map java.lang.Thread.getAllStackTraces()
public long java.lang.Thread.getId()
public java.lang.Thread$State java.lang.Thread.getState()
public static java.lang.Thread$UncaughtExceptionHandler java.lang.Thread.getDefaultUncaughtExceptionHandler()
public void java.lang.Thread.setUncaughtExceptionHandler(java.lang.Thread$UncaughtExceptionHandler)
public boolean java.lang.Object.equals(java.lang.Object)
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()
public final 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

Getting Methods of a Label Example

The following example shows the usage of java.lang.Class.getMethods() method. In this program, we've retrieved class of java.awt.label. Now using getMethods(), we've retrieved all methods and printed them.

package com.tutorialspoint;

import java.lang.reflect.Method;

public class ClassDemo {

   public static void main(String[] args) {

      try {
         Class cls = Class.forName("java.awt.Label");
         System.out.println("Methods =");
      
         /* returns the array of Method objects representing the public 
            methods of this class */
         Method m[] = cls.getMethods();
         for(int i = 0; i < m.length; i++) {
            System.out.println(m[i]);
         }
      } catch (Exception e) {
         System.out.println("Exception: " + e);
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Methods =
public void java.awt.Label.addNotify()
public int java.awt.Label.getAlignment()
public synchronized void java.awt.Label.setAlignment(int)
public java.lang.String java.awt.Label.getText()
public void java.awt.Label.setText(java.lang.String)
public javax.accessibility.AccessibleContext java.awt.Label.getAccessibleContext()
public void java.awt.Component.add(java.awt.PopupMenu)
public java.lang.String java.awt.Component.toString()
public java.lang.String java.awt.Component.getName()
public java.awt.Dimension java.awt.Component.size()
public boolean java.awt.Component.contains(int,int)
public boolean java.awt.Component.contains(java.awt.Point)
public java.awt.Container java.awt.Component.getParent()
public java.awt.Point java.awt.Component.getLocation()
public java.awt.Point java.awt.Component.getLocation(java.awt.Point)
public void java.awt.Component.remove(java.awt.MenuComponent)
public void java.awt.Component.setName(java.lang.String)
public void java.awt.Component.list()
public void java.awt.Component.list(java.io.PrintStream)
public void java.awt.Component.list(java.io.PrintStream,int)
public void java.awt.Component.list(java.io.PrintWriter)
public void java.awt.Component.list(java.io.PrintWriter,int)
public void java.awt.Component.print(java.awt.Graphics)
public java.awt.Dimension java.awt.Component.getSize()
public java.awt.Dimension java.awt.Component.getSize(java.awt.Dimension)
public void java.awt.Component.resize(int,int)
public void java.awt.Component.resize(java.awt.Dimension)
public boolean java.awt.Component.action(java.awt.Event,java.lang.Object)
public void java.awt.Component.setSize(int,int)
public void java.awt.Component.setSize(java.awt.Dimension)
public void java.awt.Component.update(java.awt.Graphics)
public boolean java.awt.Component.isOpaque()
public void java.awt.Component.enable()
public void java.awt.Component.enable(boolean)
public void java.awt.Component.disable()
public java.awt.Point java.awt.Component.location()
public final java.lang.Object java.awt.Component.getTreeLock()
public java.awt.Toolkit java.awt.Component.getToolkit()
public java.awt.peer.ComponentPeer java.awt.Component.getPeer()
public synchronized void java.awt.Component.setDropTarget(java.awt.dnd.DropTarget)
public synchronized java.awt.dnd.DropTarget java.awt.Component.getDropTarget()
public java.awt.GraphicsConfiguration java.awt.Component.getGraphicsConfiguration()
public boolean java.awt.Component.isValid()
public boolean java.awt.Component.isDisplayable()
public boolean java.awt.Component.isVisible()
public java.awt.Point java.awt.Component.getMousePosition() throws java.awt.HeadlessException
public boolean java.awt.Component.isShowing()
public boolean java.awt.Component.isEnabled()
public void java.awt.Component.setEnabled(boolean)
public boolean java.awt.Component.isDoubleBuffered()
public void java.awt.Component.enableInputMethods(boolean)
public void java.awt.Component.setVisible(boolean)
public void java.awt.Component.show()
public void java.awt.Component.show(boolean)
public void java.awt.Component.hide()
public java.awt.Color java.awt.Component.getForeground()
public void java.awt.Component.setForeground(java.awt.Color)
public boolean java.awt.Component.isForegroundSet()
public java.awt.Color java.awt.Component.getBackground()
public void java.awt.Component.setBackground(java.awt.Color)
public boolean java.awt.Component.isBackgroundSet()
public java.awt.Font java.awt.Component.getFont()
public void java.awt.Component.setFont(java.awt.Font)
public boolean java.awt.Component.isFontSet()
public java.util.Locale java.awt.Component.getLocale()
public void java.awt.Component.setLocale(java.util.Locale)
public java.awt.image.ColorModel java.awt.Component.getColorModel()
public java.awt.Point java.awt.Component.getLocationOnScreen()
public void java.awt.Component.setLocation(int,int)
public void java.awt.Component.setLocation(java.awt.Point)
public void java.awt.Component.move(int,int)
public java.awt.Rectangle java.awt.Component.getBounds()
public java.awt.Rectangle java.awt.Component.getBounds(java.awt.Rectangle)
public java.awt.Rectangle java.awt.Component.bounds()
public void java.awt.Component.setBounds(int,int,int,int)
public void java.awt.Component.setBounds(java.awt.Rectangle)
public void java.awt.Component.reshape(int,int,int,int)
public int java.awt.Component.getX()
public int java.awt.Component.getY()
public int java.awt.Component.getWidth()
public int java.awt.Component.getHeight()
public boolean java.awt.Component.isLightweight()
public void java.awt.Component.setPreferredSize(java.awt.Dimension)
public java.awt.Dimension java.awt.Component.preferredSize()
public boolean java.awt.Component.isPreferredSizeSet()
public java.awt.Dimension java.awt.Component.getPreferredSize()
public void java.awt.Component.setMinimumSize(java.awt.Dimension)
public java.awt.Dimension java.awt.Component.minimumSize()
public boolean java.awt.Component.isMinimumSizeSet()
public java.awt.Dimension java.awt.Component.getMinimumSize()
public void java.awt.Component.setMaximumSize(java.awt.Dimension)
public boolean java.awt.Component.isMaximumSizeSet()
public java.awt.Dimension java.awt.Component.getMaximumSize()
public float java.awt.Component.getAlignmentX()
public float java.awt.Component.getAlignmentY()
public int java.awt.Component.getBaseline(int,int)
public java.awt.Component$BaselineResizeBehavior java.awt.Component.getBaselineResizeBehavior()
public void java.awt.Component.doLayout()
public void java.awt.Component.layout()
public void java.awt.Component.validate()
public void java.awt.Component.invalidate()
public java.awt.Graphics java.awt.Component.getGraphics()
public java.awt.FontMetrics java.awt.Component.getFontMetrics(java.awt.Font)
public void java.awt.Component.setCursor(java.awt.Cursor)
public java.awt.Cursor java.awt.Component.getCursor()
public boolean java.awt.Component.isCursorSet()
public void java.awt.Component.paint(java.awt.Graphics)
public void java.awt.Component.paintAll(java.awt.Graphics)
public void java.awt.Component.repaint()
public void java.awt.Component.repaint(long)
public void java.awt.Component.repaint(int,int,int,int)
public void java.awt.Component.repaint(long,int,int,int,int)
public void java.awt.Component.printAll(java.awt.Graphics)
public boolean java.awt.Component.imageUpdate(java.awt.Image,int,int,int,int,int)
public java.awt.Image java.awt.Component.createImage(java.awt.image.ImageProducer)
public java.awt.Image java.awt.Component.createImage(int,int)
public java.awt.image.VolatileImage java.awt.Component.createVolatileImage(int,int)
public java.awt.image.VolatileImage java.awt.Component.createVolatileImage(int,int,java.awt.ImageCapabilities) throws java.awt.AWTException
public boolean java.awt.Component.prepareImage(java.awt.Image,java.awt.image.ImageObserver)
public boolean java.awt.Component.prepareImage
(java.awt.Image,int,int,java.awt.image.ImageObserver)
public int java.awt.Component.checkImage(java.awt.Image,java.awt.image.ImageObserver)
public int java.awt.Component.checkImage
(java.awt.Image,int,int,java.awt.image.ImageObserver)
public void java.awt.Component.setIgnoreRepaint(boolean)
public boolean java.awt.Component.getIgnoreRepaint()
public boolean java.awt.Component.inside(int,int)
public java.awt.Component java.awt.Component.getComponentAt(int,int)
public java.awt.Component java.awt.Component.getComponentAt(java.awt.Point)
public java.awt.Component java.awt.Component.locate(int,int)
public void java.awt.Component.deliverEvent(java.awt.Event)
public final void java.awt.Component.dispatchEvent(java.awt.AWTEvent)
public boolean java.awt.Component.postEvent(java.awt.Event)
public synchronized void java.awt.Component.addComponentListener(java.awt.event.ComponentListener)
public synchronized void java.awt.Component.removeComponentListener(java.awt.event.ComponentListener)
public synchronized java.awt.event.ComponentListener[] java.awt.Component.getComponentListeners()
public synchronized void java.awt.Component.addFocusListener(java.awt.event.FocusListener)
public synchronized void java.awt.Component.removeFocusListener(java.awt.event.FocusListener)
public synchronized java.awt.event.FocusListener[] java.awt.Component.getFocusListeners()
public void java.awt.Component.addHierarchyListener(java.awt.event.HierarchyListener)
public void java.awt.Component.removeHierarchyListener(java.awt.event.HierarchyListener)
public synchronized java.awt.event.HierarchyListener[] java.awt.Component.getHierarchyListeners()
public void java.awt.Component.addHierarchyBoundsListener
(java.awt.event.HierarchyBoundsListener)
public void java.awt.Component.removeHierarchyBoundsListener
(java.awt.event.HierarchyBoundsListener)
public synchronized java.awt.event.HierarchyBoundsListener[] java.awt.Component.getHierarchyBoundsListeners()
public synchronized void java.awt.Component.addKeyListener(java.awt.event.KeyListener)
public synchronized void java.awt.Component.removeKeyListener(java.awt.event.KeyListener)
public synchronized java.awt.event.KeyListener[] java.awt.Component.getKeyListeners()
public synchronized void java.awt.Component.addMouseListener(java.awt.event.MouseListener)
public synchronized void java.awt.Component.removeMouseListener(java.awt.event.MouseListener)
public synchronized java.awt.event.MouseListener[] java.awt.Component.getMouseListeners()
public synchronized void java.awt.Component.addMouseMotionListener(java.awt.event.MouseMotionListener)
public synchronized void java.awt.Component.removeMouseMotionListener
(java.awt.event.MouseMotionListener)
public synchronized java.awt.event.MouseMotionListener[] java.awt.Component.getMouseMotionListeners()
public synchronized void java.awt.Component.addMouseWheelListener(java.awt.event.MouseWheelListener)
public synchronized void java.awt.Component.removeMouseWheelListener(java.awt.event.MouseWheelListener)
public synchronized java.awt.event.MouseWheelListener[] java.awt.Component.getMouseWheelListeners()
public synchronized void java.awt.Component.addInputMethodListener(java.awt.event.InputMethodListener)
public synchronized void java.awt.Component.removeInputMethodListener
(java.awt.event.InputMethodListener)
public synchronized java.awt.event.InputMethodListener[] java.awt.Component.getInputMethodListeners()
public java.util.EventListener[] java.awt.Component.getListeners(java.lang.Class)
public java.awt.im.InputMethodRequests java.awt.Component.getInputMethodRequests()
public java.awt.im.InputContext java.awt.Component.getInputContext()
public boolean java.awt.Component.handleEvent(java.awt.Event)
public boolean java.awt.Component.mouseDown(java.awt.Event,int,int)
public boolean java.awt.Component.mouseDrag(java.awt.Event,int,int)
public boolean java.awt.Component.mouseUp(java.awt.Event,int,int)
public boolean java.awt.Component.mouseMove(java.awt.Event,int,int)
public boolean java.awt.Component.mouseEnter(java.awt.Event,int,int)
public boolean java.awt.Component.mouseExit(java.awt.Event,int,int)
public boolean java.awt.Component.keyDown(java.awt.Event,int)
public boolean java.awt.Component.keyUp(java.awt.Event,int)
public void java.awt.Component.removeNotify()
public boolean java.awt.Component.gotFocus(java.awt.Event,java.lang.Object)
public boolean java.awt.Component.lostFocus(java.awt.Event,java.lang.Object)
public boolean java.awt.Component.isFocusTraversable()
public boolean java.awt.Component.isFocusable()
public void java.awt.Component.setFocusable(boolean)
public void java.awt.Component.setFocusTraversalKeys(int,java.util.Set)
public java.util.Set java.awt.Component.getFocusTraversalKeys(int)
public boolean java.awt.Component.areFocusTraversalKeysSet(int)
public void java.awt.Component.setFocusTraversalKeysEnabled(boolean)
public boolean java.awt.Component.getFocusTraversalKeysEnabled()
public void java.awt.Component.requestFocus()
public boolean java.awt.Component.requestFocusInWindow()
public java.awt.Container java.awt.Component.getFocusCycleRootAncestor()
public boolean java.awt.Component.isFocusCycleRoot(java.awt.Container)
public void java.awt.Component.transferFocus()
public void java.awt.Component.nextFocus()
public void java.awt.Component.transferFocusBackward()
public void java.awt.Component.transferFocusUpCycle()
public boolean java.awt.Component.hasFocus()
public boolean java.awt.Component.isFocusOwner()
public void java.awt.Component.addPropertyChangeListener(java.beans.PropertyChangeListener)
public void java.awt.Component.addPropertyChangeListener
(java.lang.String,java.beans.PropertyChangeListener)
public void java.awt.Component.removePropertyChangeListener
(java.beans.PropertyChangeListener)
public void java.awt.Component.removePropertyChangeListener
(java.lang.String,java.beans.PropertyChangeListener)
public java.beans.PropertyChangeListener[] java.awt.Component.getPropertyChangeListeners()
public java.beans.PropertyChangeListener[] java.awt.Component.getPropertyChangeListeners(java.lang.String)
public void java.awt.Component.firePropertyChange(java.lang.String,byte,byte)
public void java.awt.Component.firePropertyChange(java.lang.String,char,char)
public void java.awt.Component.firePropertyChange(java.lang.String,short,short)
public void java.awt.Component.firePropertyChange(java.lang.String,long,long)
public void java.awt.Component.firePropertyChange(java.lang.String,float,float)
public void java.awt.Component.firePropertyChange(java.lang.String,double,double)
public void java.awt.Component.setComponentOrientation(java.awt.ComponentOrientation)
public java.awt.ComponentOrientation java.awt.Component.getComponentOrientation()
public void java.awt.Component.applyComponentOrientation(java.awt.ComponentOrientation)
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 boolean java.lang.Object.equals(java.lang.Object)
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()
java_lang_class.htm
Advertisements