Is there a method/function in Java that checks if another method/function is available just like function_exists(functionName) in PHP?
Here I am referring to a method/function of static class.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can find out if a method exists in Java using reflection.
Get the
Classobject of the class you’re interested in and callgetMethod()with the method name and parameter types on it.If the method doesn’t exist, it will throw a
NoSuchMethodException.Also, please note that “functions” are called methods in Java.
Last but not least: keep in mind that if you think you need this, then chances are that you’ve got a design problem at hand. Reflection (which is what the methods to inspect the actual Java classes is called) is a rather specialized feature of Java and should not generally be used in business code (although it’s used quite heavily and to some nice effects in some common libraries).