1. Create a new class, in this case i called Reflection:
/*2. Now we will try to create a inner class, i called Calculator in this class is provide 4 simple calculation method addition, substraction, division, and multiply, write like this:
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.kazao.tips.tip028;
/**
*
* @author Mr. Kazao
*/
public class Reflection {
public Reflection() {
}
public static void main(String... args) {
new Reflection();
}
}
class class Calculator {
public int add(int a, int b) {
return a + b;
}
public int sub(int a, int b) {
return a - b;
}
public double div(double a, double b) {
return a / b;
}
public int mul(int a, int b) {
return a * b;
}
}3. Now we will try to list all method runtime, fisrt make a Calculator variable like this:Calculator calc = new Calculator();and now create array of method list all available method in Calculator class:
Method[] methods = calc.getClass().getDeclaredMethods();4. Now try to print out all methods:
for (Method m : methods) {
System.out.println("-> " + m.getName());
} and we will get 4 methods like this:-> add5. Now try to get sub method:
-> sub
-> div
-> mul
Method sub = calc.getClass().getMethod("add", new Class[]{int.class, int.class});and invoke it to addition 2 number:int a = 4;we will get like this:
int b = 6;
System.out.println("Addition: " + a + " + " + b + " = " + sub.invoke(calc, new Object[]{a, b}));
Addition: 4 + 6 = 106. Ok, now we try to get div method:
Method div = calc.getClass().getMethod("div", new Class[]{double.class, double.class});and try to division 2 number:double x = 12;we will get like this:
double y = 2;
System.out.println("Division: " + x + " / " + y + " = " + div.invoke(calc, new Object[]{x, y}));
Division: 12.0 / 2.0 = 6.0
Conclusion:
Listing and invoking method in a class runtime is very simple, you will like it.
Complete Source
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.kazao.tips.tip028;
import java.lang.reflect.Method;
/**
*
* @author Mr. Kazao
*/
public class Reflection {
public Reflection() {
Calculator calc = new Calculator();
Method[] methods = calc.getClass().getDeclaredMethods();
for (Method m : methods) {
System.out.println("-> " + m.getName());
}
try {
Method sub = calc.getClass().getMethod("add", new Class[]{int.class, int.class});
int a = 4;
int b = 6;
System.out.println("Addition: " + a + " + " + b + " = " + sub.invoke(calc, new Object[]{a, b}));
Method div = calc.getClass().getMethod("div", new Class[]{double.class, double.class});
double x = 12;
double y = 2;
System.out.println("Division: " + x + " / " + y + " = " + div.invoke(calc, new Object[]{x, y}));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String... args) {
new Reflection();
}
class Calculator {
public int add(int a, int b) {
return a + b;
}
public int sub(int a, int b) {
return a - b;
}
public double div(double a, double b) {
return a / b;
}
public int mul(int a, int b) {
return a * b;
}
}
}
+++Read More+++View Complete Source +++







