TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java reflect: getDeclaredMethod, invoke

Use the java.lang.reflect namespace to use reflection. Call getDeclaredMethod and invoke.
Reflect. A Java program can look into its own structure. It can (for example) call a method by using a string for its name. This enables many powerful abilities.
Some notes. Reflection, though powerful, is not as efficient as calling a method directly. It is harder to optimize. We should reserve it for special situations.
GetDeclaredMethod example. To begin, we have a simple Java program in a "Program" class. In main, we indicate we can throw many exceptions.

Tip: The "throws" statements are auto-generated by Eclipse. You can let Eclipse add these on its own.

GetDeclaredMethod: We use this method with 1 or more arguments. The first argument is the name of the method.

Invoke: This method calls the Method reference. So the executable code in the method is run.

Java program that uses getDeclaredMethod, invoke import java.lang.reflect.*; public class Program { static void test() { // Say hello. System.out.println("Hello world"); } static void bird(String message) { // Print the argument. System.out.print("Bird says: "); System.out.println(message); } public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // Use getDeclaredMethod. // ... This gets the test method by its name. Method testMethod = Program.class.getDeclaredMethod("test"); // Invoke the test method. testMethod.invoke(null, null); // Use getDeclaredMethod. // ... Get the bird method with a first argument of String. Method birdMethod = Program.class.getDeclaredMethod("bird", String.class); // Invoke the bird method. // ... First argument is class instance, which is null for a static method. // Second argument is the actual argument String. birdMethod.invoke(null, "Seed"); } } Output Hello world Bird says: Seed
Notes, arguments. Consider the arguments to getDeclaredMethod. The first is the method name (like "test"). The second is a class argument (like String.class) if the method has an argument.

Also: If there is no argument, you can omit the class reference from the getDeclaredMethod call.

Notes, invoke. Consider now the invoke() method. It receives 2 or more arguments. For a static method, we can use null as the first argument.

And: The second argument is the actual value to be passed to the method—like a String instance.

Strings
A summary. Reflection has its place in Java programs. It can enable powerful features—like the ability for a program to analyze other programs (or itself) for flaws.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf