JAVA Reflection (A): Get Class object __java

Source: Internet
Author: User
Tags reflection wrapper
1. Get the parent class

This is the simplest and easiest way to understand--getsuperclass (), the only thing to note is that the returned must be a class, not an interface. 2. Get Inner class

There are two types of internal classes, class.getclasses () and class.getdeclaredclasses (), but they differ greatly from two, as shown in the following table:

Method Name return Results include interface include inheritance results contains only public
Class.getclasses () Gets the inner class of this class Is Is Is
Class.getdeclaredclasses () Gets the inner class of this class Is Whether Whether

Also need to add:
1. The inner class can be either a static internal class or an instance inner class;
2. Use the Getdeclaredclasses method if you want to get all of the inner classes and do not include inherited inner classes;
3. If you want to obtain all the public classes, do not include protected, private, friendly, please use the Getclasses method;
4. Internal classes can be class, interface, enum (do not ask why there is no annotation);
5. Cannot obtain anonymous class; 3. Get wrapper class from inner class

This is the inverse method of getting the inner class, the inner class to get the wrapper class (The outer Class), the method is Class.getdeclaringclass (), it should be pointed out that because the inner class can also be declared in the interface, it is possible to return the interface. 4. Anonymous class wrapper class

The instance object allows you to get the wrapper class (external Class) where the anonymous class resides, and the method is Getenclosingclass (), as the following example:

public class Reflectclasstest {public

    static void Main (string[] args) {
         thread th = new Thread () {
         };
         Class<?> clazz = Th.getclass (). Getenclosingclass ();
         The result is reflectclasstest
         System.out.println (clazz);
    }

Whether an anonymous class is inside a method or a property declaration, Getenclosingclass () can accurately find the wrapper class. 5. Other

Method Name meaning
Java.lang.reflect.Field.getDeclaringClass () Gets the Run-time assignment (non-declaration) class of a property
Java.lang.reflect.Method.getDeclaringClass () Gets the declaration return class of a method
Java.lang.reflect.Constructor.getDeclaringClass () To get a class from a constructor

Notice the difference between the Run-time assignment and the declaring class. 5. Reference Documentation

[1] Retrieving Class Objects

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.