Saturday, September 27, 2014

Reflection Example 1

import java.lang.reflect.Field;


public class Reflection {
    String str = "A";
    void test(){
        Class test=    Reflection.class;
        try {
      Field aa=      test.getDeclaredField("str");
      aa.setAccessible(true);
      try {
         
        aa.set(this, "B");
        System.out.println(str);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
   
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
    }


public static void main(String[] args) {
    Reflection test=new Reflection();
    test.test();
}
}

No comments:

Post a Comment