Java Reflection 5 Class Metot' u Cagirmak(invoke)

Reflection

public class Person {
  public int alfa(String a) {
    return a.length();
  }
  
  public static String beta(int a) {
    return "a+5="+(a+5);
  }
}
Person person  = new Person();
try {
  // instance method call
  Method method = person.getClass().getMethod("alfa",String.class);
  int a = (int) method.invoke(person, "dogukan");
  System.out.println(a);
  // static method call
  method = person.getClass().getMethod("beta",int.class);
  String out  = (String) method.invoke(null,4);
  System.out.println(out);
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}
7
a+5=9

 


Similar Posts:
  1. Java Reflection 5 Class Metot' u Cagirmak(invoke)
  2. Java Reflection 4 Class Degiskenini Degistirmek
  3. Java Reflection 3 Class' a Ait Constructor Listelemek

Tags: java, java class metot'u çağırmak, java reflction invoke, java reflection, java reflection örrneği, java reflection yansıma