Java Reflection (i): Get Class object

Source: Internet
Author: User

All the entrances to the reflection operation are java.lang.Class. In addition to java.lang.reflect.ReflectPermission, there are no common constructors for the classes below the Java.lang.reflect package. In order to obtain these classes, it is necessary to call the appropriate method of class. Object, class name, type, or existing class, these are several ways to get class.

Object.getclass ()

If you can get an instance of an object, the simplest way to get the class is to call Object.getclass (). Of course, this must be a reference type that inherits from object (not the original type).

Class c = "foo". GetClass ();

. Class Syntax

If a type is known but there are no available instances, you can use the. class syntax to get the class. This is also the simplest way to get the original type class.

Boolean B; Class C = B.getclass ();   Compile-time errorclass c = boolean.class;  Correct

Another example,

Class C = java.io.PrintStream.class;

Class.forName ()

If the permission of a class is known, you can use this static method to get the class.

But Class.forName () cannot be used for primitive types (this is a good understanding, there is no class name at all). For the name of the data type, the Class.getname has a description.

Note:
string.class.getname () returns" Java.lang.String "Byte.class.getName () returns" Byte "(new Object[3]). GetClass (). GetName () returns" [Ljava.lang.Ob ject; " (New Int[3][4][5][6][7][8][9]). GetClass (). GetName () returns "[[I] 

where for an array, it begins with one or more" [", as the depth of the array nesting. The type of the array is encoded as follows:
  
 
Element Type Encoding
Boolean Z
Byte B
Char C
Class or interface Lclassname;
Double D
Float F
Int I
Long J
Short S
So
Class Cdoublearray = Class.forName ("[D");

Represents the class of a double[].
Type property of the wrapper type of the original type
<span style= "FONT-SIZE:18PX;" >class C = double.type;</span>

Another example,

<span style= "FONT-SIZE:18PX;" >class C = void.type;</span>

Void.type is the identity of the Void.class.


A way to return to class

1, returns the class of the parent,Class.getSuperclass()

Such as

Class C = Javax.swing.JButton.class.getSuperclass ();


The parent class of JButton is Abstactbutton. Class.getClasses()

2, returns all the common classes, interfaces, enumerations, including inherited members, of the class property.

Class<?>[] C = Character.class.getClasses ();

3, returns all declared classes, interfaces, enumerations (whether public or private). Class.getDeclaredClasses()
Class<?>[] C = Character.class.getDeclaredClasses ();

The following three methods are similar,

Java.lang.reflect.Field.getDeclaringClass () Java.lang.reflect.Method.getDeclaringClass () Java.lang.reflect.Constructor.getDeclaringClass ()
Get the class of the field, method, constructor, respectively.

Import Java.lang.reflect.Field; Field f = System.class.getField ("Out"); Class C = F.getdeclaringclass ();

The field out is declared in the system.


In addition, the anonymous class does not have a "declaration" class, but there is a "contained" class.

Class C = Thread.State.class (). Getenclosingclass ();

Enumeration Thread.state is contained in the thread.


Java Reflection (i): Get Class object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.