JVM学习笔记-指向Class类的引用(A Reference to Class Class)
An instance of class java.lang.Class is created by the Java Virtual Machine for every type it loads. The virtual machine must in some way associate a reference to the Class instance for a type with the typeís data in the method area.
对于每一个被装载的类型(不管是类还是接口),虚拟机都会相应地为它创建一个java.lang.Class类的实例,而且虚拟机还必须以某种方式把这个实例和存储在方法区中的类型数据关联起来。
Your Java programs can obtain and use references to Class objects. One static method in class Class, allows you to get a reference to the Class instance for any loaded class:
在你的Java程序中,你可以得到并使用指向Class对象的引用。Class类中的一个静态方法可以让用户得到任何已装载的类的Class实例的引用。
begin
// A method declared in class java.lang.Class:
public static Class forName(String className);
end
If you invoke forName("java.lang.Object"), for example, you will get a reference to the Class object that represents java.lang.Object. If you invoke forName("java.util.Enumeration"), you will get a reference to the Class object that represents the Enumeration interface from the java.util package. You can use forName() to get a Class reference for any loaded type from any package, so long as the type can be (or already has been) loaded into the current name space. If the virtual machine is unable to load the requested type into the current name space, forName() will throw ClassNotFoundException.
比如,如果调用forName(“java.lang.Object”),那么将得到一个代表java.lang.Object的Class对象的引用。可以使用forName()来得到代表任何包中任何类型的Class对象的引用,只要这个类型可以被(或者已经被)装载到当前命名空间中。如果虚拟机无法把请求的类型装载到当前命名空间,那么forName()会抛出ClassNotFoundException异常。
An alternative way to get a Class reference is to invoke getClass() on any object reference. This method is inherited by every object from class Object itself:
另一个得到Class对象引用的方法是,可以调用任何对象引用的getClass()方法。这个方法被来自Object类本身的所有对象继承:
begin
// A method declared in class java.lang.Object:
public final Class getClass();
end
If you have a reference to an object of class java.lang.Integer, for example, you could get the Class object for java.lang.Integer simply by invoking getClass() on your reference to the Integer object.
Given a reference to a Class object, you can find out information about the type by invoking methods declared in class Class. If you look at these methods, you will quickly realize that class Class gives the running application access to the information stored in the method area. Here are some of the methods declared in class Class:
比如,如果你有一个到java.lang.integer类的对象的引用,那么你只需要简单地调用Integer对象引用的getClass()方法,就可以得到表示java.lang.Integer类的Class对象。
给出一个指向Class对象的引用,就可以通过Class类中定义的方法来找出这个类型的相关信息。如果查看这些方法,会很快意识到,Class类使得运行程序可以访问方法区中保存的信息。下面是Class类中声明的方法:
begin
// Some of the methods declared in class java.lang.Class:
public String getName();
public Class getSuperClass();
public boolean isInterface();
public Class[] getInterfaces();
public ClassLoader getClassLoader()
end
These methods just return information about a loaded type. getName() returns the fully qualified name of the type. getSuperClass() returns the Class instance for the typeís direct superclass. If the type is class java.lang.Object or an interface, none of which have a superclass, getSuperClass() returns null. isInterface() returns true if the Class object describes an interface, false if it describes a class. getInterfaces() returns an array of Class objects, one for each direct superinterface. The superinterfaces appear in the array in the order they are declared as superinterfaces by the type. If the type has no direct superinterfaces, getInterfaces() returns an array of length zero. getClassLoader() returns a reference to the ClassLoader object that loaded this type, or null if the type was loaded by the primordial class loader. All this information comes straight out of the method area.
这些方法仅能够返回已装载类型的信息。getName()返回类型的全限定名,getSupperClass()返回类型的直接超类的Class实例。如果类型是java.lang.Object类或者是一个接口,它们都没有超类,getSupperClass()返回null。isInterface()判断该类型是否是接口,如果Class对象描述一个接口就返回true;如果它描述一个类则返回false,getInterfaces()返回一个Class对象数组,其中每个Class对象对应一个直接超接口,超接口在数组中以类型声明超接口的顺序出现。如果该类型没有直接超接口,getInterfaces()则返回一个长度为零的数组。getClassLoader()返回装载该类型的ClassLoader对象的引用,如果类型是由启动类装载器装载的,则返回null。所有这些信息都直接从方法区中获得。