Java reflection (1): Get the Class Object

Source: Internet
Author: User

Java reflection (1): Get the Class Object

The entrance to all reflection operations is java. lang. Class. Except java. lang. reflect. ReflectPermission, no class under the java. lang. reflect package has a common constructor. To obtain these classes, it is necessary to call the appropriate methods of the Class. Object, Class name, type, or existing 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 the reference type inherited from the Object (the original type does not work ).

Class c = "foo".getClass();

. Class syntax

If a type is known but no instance is available, you can use the. class syntax to obtain the Class. This is also the simplest way to obtain the original Class type.

boolean b;Class c = b.getClass();   // compile-time errorClass c = boolean.class;  // correct

For example,

Class c = java.io.PrintStream.class;

Class. forName ()

If the permission name of a Class is known, you can use this static method to obtain the Class.

However, Class. forName () cannot be used for the original type ). The name of the data type is described in Class. getName.

Note:
String.class.getName()     returns "java.lang.String" byte.class.getName()     returns "byte" (new Object[3]).getClass().getName()     returns "[Ljava.lang.Object;" (new int[3][4][5][6][7][8][9]).getClass().getName()     returns "[[[[[[[I"

An array starts with one or more "[" and serves as the depth of array nesting. The array type 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
Therefore,
Class cDoubleArray = Class.forName("[D");

Indicates a double [] Class.
TYPE attribute of the original packaging TYPE
 
Class c = Double.TYPE;

For example,

Class c = Void.TYPE;

Void. TYPE is the identifier of void. class.


Method that can return Class

1. Return the Class of the parent Class,Class.getSuperclass()

For example,

Class c = javax.swing.JButton.class.getSuperclass();


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

2. return all the classes, interfaces, and enumeration of the class attributes, including inherited members.

Class
 [] c = Character.class.getClasses();

3. Return all declared classes, interfaces, and enumeration (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()
The Class that obtains the field, method, and constructor respectively.

import java.lang.reflect.Field;Field f = System.class.getField("out");Class c = f.getDeclaringClass();

Field out is declared in System.


In addition, the anonymous class does not have a "Declaration" class, but has a "included" class.

Class c = Thread.State.class().getEnclosingClass();

Enumerate Thread. State contained in Thread.


Related Article

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.