[java] 初始化顺序

修改了Think in java中的示例,使得觉得是自己迷惑的东西看上去更清晰一写,可以搞清楚java是如何对类进行初始化的


1.当使用java命令行运行一个类文件时
加载器会查找classpath下的.class文件,加载它。
如果加载过程中遇到该类的基类或用到的相关类,那么继续加载类。
知道所有的类都已经被加载。
2. 为类的对象分配内存
将所有的基本类型置为内存0
将所有引用置为null
3. 从基类到当前的类开始进行实例初始化
对每个历经的类进行定义初始化和构造器出事化
4.初始化完成。


package think.in.java.code.reusing;

class Base {
    static {
      String staticBase = "Static Base Variable";
      System.out.println(staticBase);     
    }
   
    {       
        String instanceBase = "Instance Base Variable";
        System.out.println(instanceBase);       
       
    }
   
    int uninitialized;
    int initialized = 1;
    {
        System.out.println("instance of Base: uninitialized = " + uninitialized);
        System.out.println("instance of Base: initialized = " + initialized);
    }
   
    Base(){
        System.out.println("constructor of Base: Base()");
        System.out.println("constructor of Base: uninitialized = " + uninitialized);
        System.out.println("constructor of Base: initialized = " + initialized);
        System.out.println("constructor of Base: getInitialized() = " + getInitialized());
    }
   
    int getUnitialized(){
        return uninitialized;
    }
    int getInitialized(){
        return initialized;
    }
}

class Derived extends Base{
    static {
      String staticDerived = "Static Derived Variable";
      System.out.println(staticDerived);
      Component staticComponet = new Component();
    }
    {
        String instanceDerived = "Instance Derived Variable";
        System.out.println(instanceDerived);
        Component instanceComponet = new Component();
    }
   
    int uninitialized;
    int initialized = 3;
    {
        System.out.println("instance of Derived: uninitialized = " + uninitialized);
        System.out.println("instance of Derived: initialized = " + initialized);
    }
    Derived(){
        System.out.println("constructor of Derived: Derived");
        System.out.println("constructor of Derived: uninitialized = " + uninitialized);
        System.out.println("constructor of Derived: initialized = " + initialized);
        System.out.println("constructor of Derived: getInitialized() = " + getInitialized());
    }
    int intUnitialized(){
        return uninitialized;
    }
    int getInitialized(){
        return initialized;
    }
}

class Component {
    static {
          String staticComponent = "Static Component Variable";
          System.out.println(staticComponent);
        }
        {
            String instanceComponent = "Instance Component Variable";
            System.out.println(instanceComponent);
        }
       
        int uninitialized;
        int initialized = 2;
        {
            System.out.println("instance of Component: uninitialized = " + uninitialized);
            System.out.println("instance of Component: initialized = " + initialized);
        }
        Component(){
            System.out.println("constructor of Component: Component");
            System.out.println("constructor of Component: uninitialized = " + uninitialized);
            System.out.println("constructor of Component: initialized = " + initialized);
            System.out.println("constructor of Component: getInitialized() = " + getInitialized());
        }
        int intUnitialized(){
            return uninitialized;
        }
        int getInitialized(){
            return initialized;
        }
}

public class InitializeOrder {

    public static void main(String[] args) {
        Base base = new Derived();
    }

}
/*Output:
Static Base Variable
Static Derived Variable
Static Component Variable
Instance Component Variable
instance of Component: uninitialized = 0
instance of Component: initialized = 2
constructor of Component: Component
constructor of Component: uninitialized = 0
constructor of Component: initialized = 2
constructor of Component: getInitialized() = 2
Instance Base Variable
instance of Base: uninitialized = 0
instance of Base: initialized = 1
constructor of Base: Base()
constructor of Base: uninitialized = 0
constructor of Base: initialized = 1
constructor of Base: getInitialized() = 0
Instance Derived Variable
Instance Component Variable
instance of Component: uninitialized = 0
instance of Component: initialized = 2
constructor of Component: Component
constructor of Component: uninitialized = 0
constructor of Component: initialized = 2
constructor of Component: getInitialized() = 2
instance of Derived: uninitialized = 0
instance of Derived: initialized = 3
constructor of Derived: Derived
constructor of Derived: uninitialized = 0
constructor of Derived: initialized = 3
constructor of Derived: getInitialized() = 3
*/ 

你可能感兴趣的:(C++,Java,java,constructor,string,class,output,null)