码迷,mamicode.com
首页 > 编程语言 > 详细

Java reflection

时间:2015-01-06 17:48:11      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

Object Reflection is a feature in Java which provides a way to get reflective information about
Java classes and objects, such as:
1 Getting information about methods and fields present inside the class at run time
2 Creating a new instance of a class
3 Getting and setting the object fields directly by getting field reference, regardless of
what the access modifier is.

 

Why it is useful:
1 Helps in observing or manipulating the runtime behavior of applications
2 Useful while debugging and testing applications, as it allows direct access to methods,
constructors, fields, etc

try {
            Class c = Class.forName("java.util.ArrayList");
            Method m[] = c.getDeclaredMethods();
            for (int i = 0; i < m.length; i++) {
                System.out.println(m[i].toString());
            }
            System.out.println();
            Field[] f = c.getDeclaredFields();
            for (int i = 0; i < f.length; i++) {
                System.out.println(f[i].toString());
            }
        } catch (Throwable e) {
            System.err.println(e);
        }

 

Java reflection

标签:

原文地址:http://www.cnblogs.com/phoenix13suns/p/4206460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!