Get the list of all the declared methods in Java


The list of all the declared methods can be obtained using the java.lang.Class.getDeclaredMethods() method. This method returns an array that contains all the Method objects with public, private, protected and default access. However, the inherited methods are not included.

Also, the getDeclaredMethods() method returns a zero-length array if the class or interface has no methods or if a primitive type, array class or void is represented in the Class object.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.lang.reflect.Method;
public class Main {
   public static void main(String[] argv) throws Exception {
      Class c = java.lang.String.class;
      Method[] methods = c.getDeclaredMethods();
      for(int i = 0; i < methods.length; i++) {
         System.out.println("The method is: " + methods[i].toString());
      }
   }
}

Output

The method is: public boolean java.lang.String.equals(java.lang.Object)
The method is: public java.lang.String java.lang.String.toString()
The method is: public int java.lang.String.hashCode()
The method is: public int java.lang.String.compareTo(java.lang.Object)
The method is: public int java.lang.String.compareTo(java.lang.String)
The method is: public int java.lang.String.indexOf(java.lang.String,int)
The method is: static int java.lang.String.indexOf(char[],int,int,java.lang.String,int)
The method is: static int java.lang.String.indexOf(char[],int,int,char[],int,int,int)
The method is: public int java.lang.String.indexOf(int)
The method is: public int java.lang.String.indexOf(java.lang.String)
The method is: public int java.lang.String.indexOf(int,int)
The method is: public static java.lang.String java.lang.String.valueOf(char)
The method is: public static java.lang.String java.lang.String.valueOf(java.lang.Object)
The method is: public static java.lang.String java.lang.String.valueOf(boolean)
The method is: public static java.lang.String java.lang.String.valueOf(char[],int,int)
The method is: public static java.lang.String java.lang.String.valueOf(char[])
The method is: public static java.lang.String java.lang.String.valueOf(double)
The method is: public static java.lang.String java.lang.String.valueOf(float)
The method is: public static java.lang.String java.lang.String.valueOf(long)
The method is: public static java.lang.String java.lang.String.valueOf(int)
The method is: private static void java.lang.String.checkBounds(byte[],int,int)
The method is: public int java.lang.String.length()
The method is: public boolean java.lang.String.isEmpty()
The method is: public char java.lang.String.charAt(int)
The method is: public int java.lang.String.codePointAt(int)
The method is: public int java.lang.String.codePointBefore(int)
The method is: public int java.lang.String.codePointCount(int,int)
The method is: public int java.lang.String.offsetByCodePoints(int,int)
The method is: public void java.lang.String.getChars(int,int,char[],int)
The method is: void java.lang.String.getChars(char[],int)
The method is: public byte[] java.lang.String.getBytes()
The method is: public byte[] java.lang.String.getBytes(java.lang.String) throws java.io.UnsupportedEncodingException
The method is: public void java.lang.String.getBytes(int,int,byte[],int)
The method is: public byte[] java.lang.String.getBytes(java.nio.charset.Charset)
The method is: public boolean java.lang.String.contentEquals(java.lang.StringBuffer)
The method is: public boolean java.lang.String.contentEquals(java.lang.CharSequence)
The method is: private boolean java.lang.String.nonSyncContentEquals(java.lang.AbstractStringBuilder)
The method is: public boolean java.lang.String.equalsIgnoreCase(java.lang.String)
The method is: public int java.lang.String.compareToIgnoreCase(java.lang.String)
The method is: public boolean java.lang.String.regionMatches(int,java.lang.String,int,int)
The method is: public boolean java.lang.String.regionMatches(boolean,int,java.lang.String,int,int)
The method is: public boolean java.lang.String.startsWith(java.lang.String)
The method is: public boolean java.lang.String.startsWith(java.lang.String,int)
The method is: public boolean java.lang.String.endsWith(java.lang.String)
The method is: private int java.lang.String.indexOfSupplementary(int,int)
The method is: public int java.lang.String.lastIndexOf(int,int)
The method is: static int java.lang.String.lastIndexOf(char[],int,int,char[],int,int,int)
The method is: static int java.lang.String.lastIndexOf(char[],int,int,java.lang.String,int)
The method is: public int java.lang.String.lastIndexOf(java.lang.String,int)
The method is: public int java.lang.String.lastIndexOf(int)
The method is: public int java.lang.String.lastIndexOf(java.lang.String)
The method is: private int java.lang.String.lastIndexOfSupplementary(int,int)
The method is: public java.lang.String java.lang.String.substring(int)
The method is: public java.lang.String java.lang.String.substring(int,int)
The method is: public java.lang.CharSequence java.lang.String.subSequence(int,int)
The method is: public java.lang.String java.lang.String.concat(java.lang.String)
The method is: public java.lang.String java.lang.String.replace(char,char)
The method is: public java.lang.String java.lang.String.replace(java.lang.CharSequence,java.lang.CharSequence)
The method is: public boolean java.lang.String.matches(java.lang.String)
The method is: public boolean java.lang.String.contains(java.lang.CharSequence)
The method is: public java.lang.String java.lang.String.replaceFirst(java.lang.String,java.lang.String)
The method is: public java.lang.String java.lang.String.replaceAll(java.lang.String,java.lang.String)
The method is: public java.lang.String[] java.lang.String.split(java.lang.String,int)
The method is: public java.lang.String[] java.lang.String.split(java.lang.String)
The method is: public static java.lang.String java.lang.String.join(java.lang.CharSequence,java.lang.CharSequence[])
The method is: public static java.lang.String java.lang.String.join(java.lang.CharSequence,java.lang.Iterable)
The method is: public java.lang.String java.lang.String.toLowerCase(java.util.Locale)
The method is: public java.lang.String java.lang.String.toLowerCase()
The method is: public java.lang.String java.lang.String.toUpperCase()
The method is: public java.lang.String java.lang.String.toUpperCase(java.util.Locale)
The method is: public java.lang.String java.lang.String.trim()
The method is: public char[] java.lang.String.toCharArray()
The method is: public static java.lang.String java.lang.String.format(java.util.Locale,java.lang.String,java.lang.Object[])
The method is: public static java.lang.String java.lang.String.format(java.lang.String,java.lang.Object[])
The method is: public static java.lang.String java.lang.String.copyValueOf(char[],int,int)
The method is: public static java.lang.String java.lang.String.copyValueOf(char[])
The method is: public native java.lang.String java.lang.String.intern()

Updated on: 25-Jun-2020

682 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements