The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
The entry point for all reflection operations is java.lang.Class
. With the exception of java.lang.reflect.ReflectPermission
, none of the classes in java.lang.reflect
have public constructors. To get to these classes, it is necessary to invoke appropriate methods on Class
. There are several ways to get a Class
depending on whether the code has access to an object, the name of class, a type, or an existing Class
.
If an instance of an object is available, then the simplest way to get its Class
is to invoke Object.getClass()
. Of course, this only works for reference types which all inherit from Object
. Some examples follow.
Class c = "foo".getClass();
Class c = System.console().getClass();
There is a unique console associated with the virtual machine which is returned by the static
method System.console()
. The value returned by getClass()
is the Class
corresponding to java.io.Console
.
enum E { A, B } Class c = A.getClass();
A
is an instance of the enum E
; thus getClass()
returns the Class
corresponding to the enumeration type E
.
byte[] bytes = new byte[1024]; Class c = bytes.getClass();
Since arrays are Objects
, it is also possible to invoke getClass()
on an instance of an array. The returned Class
corresponds to an array with component type byte
.
import java.util.HashSet; import java.util.Set; Set<String> s = new HashSet<String>(); Class c = s.getClass();
In this case, java.util.Set
is an interface to an object of type java.util.HashSet
. The value returned by getClass()
is the class corresponding to java.util.HashSet
.
If the type is available but there is no instance then it is possible to obtain a Class
by appending ".class"
to the name of the type. This is also the easiest way to obtain the Class
for a primitive type.
boolean b; Class c = b.getClass(); // compile-time error Class c = boolean.class; // correct
Note that the statement boolean.getClass()
would produce a compile-time error because a boolean
is a primitive type and cannot be dereferenced. The .class
syntax returns the Class
corresponding to the type boolean
.
Class c = java.io.PrintStream.class;
The variable c
will be the Class
corresponding to the type java.io.PrintStream
.
Class c = int[][][].class;
The .class
syntax may be used to retrieve a Class
corresponding to a multi-dimensional array of a given type.
If the fully-qualified name of a class is available, it is possible to get the corresponding Class
using the static method Class.forName()
. This cannot be used for primitive types. The syntax for names of array classes is described by Class.getName()
. This syntax is applicable to references and primitive types.
Class c = Class.forName("com.duke.MyLocaleServiceProvider");
This statement will create a class from the given fully-qualified name.
Class cDoubleArray = Class.forName("[D"); Class cStringArray = Class.forName("[[Ljava.lang.String;");
The variable cDoubleArray
will contain the Class
corresponding to an array of primitive type double
(that is, the same as double[].class
). The cStringArray
variable will contain the Class
corresponding to a two-dimensional array of String
(that is, identical to String[][].class
).
The .class
syntax is a more convenient and the preferred way to obtain the Class
for a primitive type; however there is another way to acquire the Class
. Each of the primitive types and void
has a wrapper class in java.lang
that is used for boxing of primitive types to reference types. Each wrapper class contains a field named TYPE
which is equal to the Class
for the primitive type being wrapped.
Class c = Double.TYPE;
There is a class java.lang.Double
which is used to wrap the primitive type double
whenever an Object
is required. The value of Double.TYPE
is identical to that of double.class
.
Class c = Void.TYPE;
Void.TYPE
is identical to void.class
.
There are several Reflection APIs which return classes but these may only be accessed if a Class
has already been obtained either directly or indirectly.
Class.getSuperclass()
Class c = javax.swing.JButton.class.getSuperclass();
javax.swing.JButton
is javax.swing.AbstractButton
.Class.getClasses()
Class<?>[] c = Character.class.getClasses();
Character
contains two member classes Character.Subset
and Character.UnicodeBlock
.Class.getDeclaredClasses()
Class<?>[] c = Character.class.getDeclaredClasses();
Character
contains two public member classes Character.Subset
and Character.UnicodeBlock
and one private class Character.CharacterCache
.Class.getDeclaringClass()
java.lang.reflect.Field.getDeclaringClass()
java.lang.reflect.Method.getDeclaringClass()
java.lang.reflect.Constructor.getDeclaringClass()
Class
in which these members were declared. Anonymous Class Declarations will not have a declaring class but will have an enclosing class. import java.lang.reflect.Field; Field f = System.class.getField("out"); Class c = f.getDeclaringClass();
out
is declared in System
. public class MyClass { static Object o = new Object() { public void m() {} }; static Class<c> = o.getClass().getEnclosingClass(); }
o
is null
.Class.getEnclosingClass()
Class c = Thread.State.class().getEnclosingClass();
Thread.State
is Thread
. public class MyClass { static Object o = new Object() { public void m() {} }; static Class<c> = o.getClass().getEnclosingClass(); }
o
is enclosed by MyClass
.