In java it can be done through the reflection api.
Have a look at Class.getMethod(String methodName, Class... parameterTypes).
A complete example (of a non-static method with an argument) would be:
import java.lang.reflect.*;
public class Test {
public String methodName(int i) {
return "Hello World: " + i;
}
public static void main(String... args) throws Exception {
Test t = new Test();
Method m = Test.class.getMethod("methodName", int.class);
String returnVal = (String) m.invoke(t, 5);
System.out.println(returnVal);
}
}
Which outputs:
Hello World: 5
Calling a method named "string" at runtime in Java and C - Stack Overflow
Java String Methods - You Need To Know
First method of the article is charAt... Great start! Then you realise this is probably every method available order alphabetically... This could be reduce to a "5 methods to know" to be really interesting.
More on reddit.comELI5:What is a "String" in Java/programming and why is it known as a "complex added type" and other variable are known as "primitive" ?
New Methods on Java Strings With JDK 11
Looks like they're taking the awful PHP "real_escape_string" approach to API development of finding increasingly strained synonyms when something needs to be fixed by making the Unicode-aware version of String.trim() be named String.strip() instead of just adding an overload for trim() that takes an option parameter to specify the trimming type.
A hypothetical alternative of trim(StringTrimOptions options), used as in these examples:
// First two are equivalent, and use the backward-compatible
// pre-existing trimming logic
str.trim();
str.trim(StringTrimOptions.ASCII_WHITESPACE);
// This example uses new Unicode-compatible trimming logic.
str.trim(StringTrimOptions.UNICODE_WHITESPACE);
... would be a much better solution that wouldn't require every developer going forward, in perpetuity, to have to discover what the non-obvious difference between trim() and strip() is (whereas the hypothetical StringTrimOptions alternative is self-explanatory to a large extent); and is forward-compatible should the need ever arise in the future for yet another type of string trimming.
(And with the isBlank() method being added too, which could probably also benefit from being able to specify the type of whitespace; maybe instead of StringSplitOptions the argument should be WhitespaceType instead so an overload ofisBlank() could read correctly with it too.)
Videos
In java it can be done through the reflection api.
Have a look at Class.getMethod(String methodName, Class... parameterTypes).
A complete example (of a non-static method with an argument) would be:
import java.lang.reflect.*;
public class Test {
public String methodName(int i) {
return "Hello World: " + i;
}
public static void main(String... args) throws Exception {
Test t = new Test();
Method m = Test.class.getMethod("methodName", int.class);
String returnVal = (String) m.invoke(t, 5);
System.out.println(returnVal);
}
}
Which outputs:
Hello World: 5
In Java you would use reflection:
Class<?> classContainingTheMethod = ...; // populate this!
Method stringMethod = classContainingTheMethod.getMethod("string");
Object returnValue = stringMethod.invoke(null);
This is a very simple case that assumes your method is static and takes no parameters. For non-static methods, you'd pass in the instance to invoke the method on, and in any case you can pass in any required parameters to the invoke() method call.