Active reference and passive reference

Active reference and passive reference

1.new objects, reflection acquisition class objects are all active references

Active reference

1. The main () method is the entry point of the program, the function starts here

2. You take the initiative to initialize an object definitely new!

3. The call to the non-static page will be initialized, and a total of final constants will come out. No matter what variable is called in a class, the method will instantiate this variable. Except for final constants, all methods depend on this class.

4. Reflection wants to get the class object, you don't initialize, how can there be class?

5. Because the subclass must know what method the parent class writes, it is necessary to look at the property of the parent class (methods and variables)

package Reflection;
public class Tets04 {
    static {
        System.out.println ("main类被引用");
    }
    public static void main(String[] args) throws ClassNotFoundException {
//        Son son = new Son ();//初始化一个类,会先初始化他的父类
//        Class. forName ("Reflection.Son");
        //不会产生类的引用
        System.out.println (Son.b);//子类调用父类的变量,子类不会被加载
        Son[] arr = new Son[10];//只是一片数组,并不会加载类
        System.out.println (Son.M);//常量和静态在链接过程中已经调入常量池之中了
    }
}
class Father{
    static int b = 2;
    static {
        System.out.println ("父类被加载");
    }
}
class Son extends Father{
    static {
        System.out.println ("子类被加载");
        m = 300;
    }
    static int m = 100;
    static final int M = 1;
}

Guess you like

Origin www.cnblogs.com/li33/p/12727424.html