Reflection mechanism of Java

Tinker Bell is not lazy 2022-02-13 09:09:18 阅读数:134

reflection mechanism java

Short step , A thousand miles ; Don't product the little stream , Beyond into the sea . Let's settle down , Poetry and travel expenses from afar are really expensive !

Java The reflection mechanism of


principle :1. class : You can know all the properties and methods in the class .

2. object : You can call any method .

notes : Set the hair color to red .

3. Everything is the object , Class is also an object .( The key point is )

4. .class In the hard disk is a file , When its operation is loaded into memory , It becomes an object .

.class Object when loaded into memory (class object ) and Instantiate objects The difference between :

  1. At run time , The type information of a class is determined by class Object represented , Contains information about the class .
  2. Instantiate objects , Namely class Object instantiation .

effect :1. Get information dynamically .

2. Dynamically calling object methods .

application :1. Generate dynamic agents .

2. Slice oriented programming (AOP).

​ == notes :== Slice oriented programming , Roughly means that the object can be in Runtime 、 Compile time 、 Generated when class is loaded .


  • Using the reflex mechanism , Implement the creation of a class and call its methods .

    • Test.class
    package com.hnucm;
    // Called class 
    public class Test {
    public void fun() {
    System.out.println(" This is a test class ");
    • Impl.class
    package com.hnucm;
    import java.lang.reflect.Method;
    public class Impl {
    // java Reflection to call the methods of the class 
    public static void main(String[] args) throws Exception {
    // Get class , Class loader 
    // The parameter is the package name + Class name 
    Class c = Class.forName("com.hnucm.Test");
    // Get a specific method get a specific method 
    // adopt : Method name + Parameter list 
    Method m = c.getDeclaredMethod("fun");
    // Execute through reflection mechanism fun Method .
    // Get the class object , Create by reflection 
    Object o = c.newInstance();
    // call o Object's m Method , Here is the Test Object's fun Method 
    Object retValue = m.invoke(o);

obtain class Three ways of objects

obtain class Three ways of objects :1. new Object().getClass

2. Object.class

3. Class.forName(“java.util.String”)

The difference between the three ways :

1. Obtained by the first way class object , Will call all code and code blocks .

2. The object obtained in the second way , No code or code blocks will be called .

3. Through the third way , Only the contents of static code blocks will be called .

4. Static code blocks, no matter who executes , It will only be initialized once .

  • Judge three ways to get Class Whether the object is the same .

    • Impl.class
    package com.hnucm;
    public class Impl {
    //java Reflect to get class object 
    public static void main(String[] args) throws Exception{
    // Get objects in three ways 
    Class c1 = new Test().getClass();
    Class c2 = Test.class;
    Class c3 = Class.forName("com.hnucm.Test");
    // Judge whether it is the same 
    System.out.println(c1 == c2);
    System.out.println(c2 == c3);

Three different ways to get the same object .( Corresponding in heap memory class object )

copyright:author[Tinker Bell is not lazy],Please bring the original link to reprint, thank you.