자바 초기 화, 동적 정적 초기 화 블록, 동적 정적 변수 실행 순서 에 대한 memo

package com.hxw.T2;

public class ExtendsMemo {

	public static void main(String[] args) {
		/*
		 *     
		 *  、      (Static)
		 * 	1、          ,            
		 * 	2、                        
		 * 	3、                          ,           
		 *  、     
		 * 	1、                  
		 * ······                        ······
		 *  、      
		 * 	1、                          
		 *  、      
		 * 	1、                        
		 * ······                         ······
		 *   :    ,          ,       ,        
		 * 	1、                         
		 * 	2、                         
		 * 	3、      
		 * 	            2、3   
		 * 
		 * ············                    ·········
		 *  、       ,        
		 * 	1、                    ,                    
		 * 	2、                     ,        
		 * 	3、                     ,        
		 *  :                    ,                             
		 */	
		int i = 0;
		switch(i) {
		case 0:
			
			new A();
			new A();
			
			break;
		case 1:
			
			A ab = new B();
			
			break;
		case 2 :
			
			C c1 = new C();
			System.out.println("c.i1 = " + c1.i1);//2
			System.out.println("c.i2 = " + c1.i2);//3
			
			break;
		} 
        /*  i=2    
         *     2 3 ?
         *              :    c1        
         *       t1,i1,i2    null,0,0
         *          new C(),              c
         * 		              ,    c       
         * 		  i1++,i2++   ,i1,i2   1
         *      int i1;      , i1,i2     1
         *      int i2 = 2 i2   2, i1 = 1,i2 = 2
         *      c1     ,i1,i2   ++,  i1 = 2,i2 = 3
         *   i1,i2,    :c1.i1 = 2,c1.i2 = 3
         *                                 。
         */
	}
}

class A {
	public A() {
		System.out.println("construct A");
		m();
		System.out.println();
	}
	
	private int ai = getAI();
	
	{
		System.out.println("dynamic block in A");
	}

	static {
		System.out.println("static block in A");
	}
	
	private static int asi = getASI();
	
	static int getASI() {
		System.out.println("initial static int asi");
		System.out.println();
		return 5;
	}
	
	static int getAI() {
		System.out.println("initial int ai");
		return 10;
	}
	
	public void m() {
		System.out.println("A.m()");
	}
}

class B extends A{
	public B() {
		System.out.println("construct B");
		super.m();
		m();
	}
	
	{
		System.out.println("dynamic block in B");	
	}
	private int bi = getBI();
	private static int bsi = getBSI();

	
	static {
		System.out.println("static block in B");
		System.out.println();
	}
	
	static int getBSI() {
		System.out.println("initial static int bsi");
		return 5;
	}
	
	static int getBI() {
		System.out.println("initial int bi");
		return 10;
	}
	
	public void m() {
		System.out.println("B.m() bi=" + bi);
	}
}

class C{
    private static C c = new C();
    public static int i1;
    public static int i2 = 2;
    
    public C(){
        i1++;
        i2++;
    }
}

 다음으로 이동:http://blog.csdn.net/shijinupc/article/details/7692315

좋은 웹페이지 즐겨찾기