Write a Program to Find out ReflectionAPI Java Example Program?
package com.java.Softwaretestingblog; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; class Axy { @SuppressWarnings("unused") private void cube(String n) { System.out.println(n); } } public class Reflection_APIEx { public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // TODO Auto-generated method stub @SuppressWarnings("rawtypes") Class c=Axy.class; Object obj=c.newInstance(); @SuppressWarnings("unchecked") Method m=c.getDeclaredMethod("cube",new Class[]{String.class}); m.setAccessible(true); m.invoke(obj,"SoftwareTestingBlog"); } }
Output:
SoftwareTestingBlog
Leave a Reply