Basics of Java Reflection Methods - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

Basics of Java Reflection Methods

Share This

Methods can be referenced by using the Method class. The following example shows the details. Reflection helps to get the method name, return type and parameter types. You can also invoke methods using this technique.
Content of TestMain.java
package com.t4b.test; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Type; public class TestMain { static Class c; public static void main(String[] args) { try { c = Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println("Methods:"); for (Method m : c.getMethods()) { System.out.println("Method: " + m); System.out.println(" Return Type: " + m.getReturnType()); for (Type t : m.getParameterTypes()) System.out.println(" Arguments: " + t.toString()); } } }

Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.