Spec-Zone .ru
спецификации, руководства, описания, API
|
The entry point for all reflection operations is
java.lang.Class
. With the exception of
java.lang.reflect.ReflectPermission
java.lang.reflect
Class
Class
Class
If an instance of an object is available, then the simplest way to get its
Class
Object.getClass()
Object
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()
getClass()
Class
java.io.Console
enum E { A, B } Class c = A.getClass();
A
is is an instance of the enum E
; thus
getClass()
Class
E
.
byte[] bytes = new byte[1024]; Class c = bytes.getClass();
Since arrays are
Objects
getClass()
Class
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
java.util.HashSet
getClass()
java.util.HashSet
If the type is available but there is no instance then it is possible to obtain a
Class
".class"
to the name of the type. This is also the easiest way to obtain the
Class
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
boolean
.
Class c = java.io.PrintStream.class;
The variable c
will be the
Class
java.io.PrintStream
Class c = int[][][].class;
The .class
syntax may be used to retrieve a
Class
If the fully-qualified name of a class is available, it is possible to get the corresponding
Class
Class.forName()
Class.getName()
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
double
(i.e. the same as double[].class
). The cStringArray
variable will contain the
Class
String
String[][].class
).
The .class
syntax is a more convenient and the preferred way to obtain the
Class
Class
void
has a wrapper class in
java.lang
TYPE
which is equal to the
Class
Class c = Double.TYPE;
There is a class
java.lang.Double
double
whenever an
Object
Double.TYPE
double.class
.
Class c = Void.TYPE;
Void.TYPE
void.class
.
There are several Reflection APIs which return classes but these may only be accessed if a
Class
Class.getSuperclass()
Class c = javax.swing.JButton.class.getSuperclass();
javax.swing.JButton
javax.swing.AbstractButton
Class.getClasses()
Class<?>[] c = Character.class.getClasses();
Character
Character.Subset
Character.UnicodeBlock
Class.getDeclaredClasses()
Class<?>[] c = Character.class.getDeclaredClasses();
Character
Character.Subset
Character.UnicodeBlock
Character.CharacterCache
.Class.getDeclaringClass()
java.lang.reflect.Field.getDeclaringClass()
java.lang.reflect.Method.getDeclaringClass()
java.lang.reflect.Constructor.getDeclaringClass()
Class
import java.lang.reflect.Field; Field f = System.class.getField("out"); Class c = f.getDeclaringClass();
out
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
Thread
public class MyClass { static Object o = new Object() { public void m() {} }; static Class<c> = o.getClass().getEnclosingClass(); }
o
is enclosed by MyClass
.