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
    }
    
    //顺序:
    //首先是静态对象
    //非静态对象
    //构造函数
    

      

  • 相关阅读:
    第二次作业
    《自动化技术中的进给电气传动》读书笔记1.1-1.2
    证券投资分析
    微信官方文档概述
    联想拯救者Y7000电池无法充电问题
    Ubuntu用户权限管理
    证券市场基础知识
    Markdown All in One使用教程
    Markdown学习笔记
    第九周
  • 原文地址:https://www.cnblogs.com/E-star/p/3200241.html
Copyright © 2011-2022 走看看