When you overload a Java method, you write multiple methods with a shared name. code example

Example 1: how do you make a method that returns an array java

public static int[] numbers()  
{  
int[] arr={5,6,7,8,9};  //initializing array  
return arr;  
}

Example 2: get method of a class which I only have string to

String className = "my.class.name"; //Class name

Object classObj = null;
try {
  classObj = Class.forName(className);
} catch (ClassNotFoundException e) {
  e.printStackTrace();
}

Method classMethod = null;
try {
  classMethod = classObj.getClass().getMethod("method", param1.class, param2.class, ..);
} catch (SecurityException | NoSuchMethodException e) {
  e.printStackTrace();
}

try {
  classMethod.invoke(classObj, arg1, args2, ..);
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException) {
  e.printStackTrace();
}

Tags:

Java Example