开发者

Question about reflection package

I read from sun website that for every reference jvm is creating one immutable class object so that it can introspect the 开发者_开发知识库run time information of every class. And sun has mentioned to use .class syntax. I want to know the internal mechanism of this syntax and how it works.


You may want to start with the reflection tutorial

The .class syntax is explained on this page (no, it doesn't explain the inner workings)


If compiled with -target 1.4 or earlier, Class.forName(String) is called once and then Class reference stored in a synthetic static field in the calling class. For -target 1.5 and later, a new version of the ldc ("load constant") bytecode operation references the class.

Use javap -c to see the bytecode that javac produces.


For each non-generic (or raw) type (Class, interface, array-type, primitive type) there is a Class object, created when this class is loaded. This object is not totally immutable, as it contains the static variables of the class, for example.

If you have an object, you can get the class object of its implementing class by calling o.getClass(). If you have some type, you can get its class object by T.class in Java.

From the class object you can inspect your class, to get constructors, methods, fields, superclass, implemented interfaces, and so on - this is called reflection.

(See the links in the other answers for more details.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜