Home » Core Java » reflection » Invoke Method with reflection

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Invoke Method with reflection

With this example we are going to demonstrate how to invoke a method using reflection. In short, to invoke a method using reflection you should:

  • Create a new StringBuilder with no characters in it and an initial capacity of 16 characters.
  • Append a specified String to the builder using append(String str) API method of StringBuilder.
  • Use getClass() API method to get the runtime class of the StringBuilder and then getMethod(String name, Class<?>... parameterTypes) API method of Class to get the Method object that reflects the specified public member method of the class or interface represented by this Class object.
  • Use invoke(Object obj, Object... args) API method to invoke the underlying method represented by this Method object, on the specified object with the specified parameters. Individual parameters are automatically unwrapped to match primitive formal parameters, and both primitive and reference parameters are subject to method invocation conversions as necessary.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.lang.reflect.Method;

public class InvokeMethodWithReflection {
	
	public static void main(String[] args) throws Exception {
		
		StringBuilder sb = new StringBuilder();
		
		sb.append("Java Code Geeks");
		System.out.println("Initial: " + sb);
		
		// retrieve the method named "append"
		Method appendMethod = sb.getClass().getMethod("append", String.class);
		
		// invoke the method with the specified argument
		appendMethod.invoke(sb, "Java Examples & Code Snippets");
		
		System.out.println("Final: " + sb);
		
	}

}

Output:

Initial: Java Code Geeks
Final: Java Code GeeksJava Examples & Code Snippets

 
This was an example of how to invoke a method using reflection in Java.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of