zoukankan      html  css  js  c++  java
  • Java中含有静态成员的的初始化顺序

    class Bowl{
    	Bowl(int marker){
    		System.out.println("Bowl(" + marker + ")" );
    	}
    	void f1(int marker){
    		System.out.println("f1(" + marker + ")");
    	}
    }
    
    class Table{
    	//首先是静态变量
    	static Bowl bow1 = new Bowl(1);//2
    	Table(){
    		System.out.println("Table()");//4
    		bowl2.f1(1);//5
    	}
    	void f2(int marker){
    		System.out.println("f2(" + marker + ")");
    	}
    	static Bowl bowl2 = new Bowl(2);//3
    }
    
    class CupBoard{
    	Bowl bowl3 = new Bowl(3);//8  不是static变量,会与每一个对象绑定,所以每创建一次对象都会初始化一次
    	static Bowl bowl4 = new Bowl(4);//6
    	CupBoard(){
    		System.out.println("CupBoard");//9
    		bowl4.f1(2);//10
    	}
    	static Bowl bowl5 = new Bowl(5);//7
    	void f3(int marker){
    		System.out.println("f3(" + marker + ")");
    	}
    }
    public class Main {
    	
    	//首先初始化静态变量
    	public static void main(String[] args) {
    		System.out.println("Creating new CupBoard in main");
    		new CupBoard();//11
    		System.out.println("Creating new CupBoard in main");
    		new CupBoard();//12
    		table.f2(1);//13
    		cupBoard.f3(1);//14
    	}
    	static Table table = new Table();//1
    	static CupBoard cupBoard = new CupBoard();//5
    }
    
    //顺序:
    //首先是静态对象
    //非静态对象
    //构造函数
    

      

  • 相关阅读:
    ios web 媒体查询兼容
    Linux python 虚拟环境管理
    three.js 纹理动画实现
    three.js 在模型上移动相机
    three.js 模型拖动之DragControls控制器
    three.js 添加html内容、文本
    微信公众号对接记录
    事务的日志
    事务的隔离级别
    事务中的锁
  • 原文地址:https://www.cnblogs.com/E-star/p/3200241.html
Copyright © 2011-2022 走看看