java中的static问题

来源:百度知道 编辑:UC知道 时间:2024/06/14 09:33:31
package com.nanj;

public class Jingtai {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("11111111111111");
new Cupboard();
System.out.println("11111111111111");
new Cupboard();
t2.f2(1);
t3.f3(1);

}
static Table t2 = new Table();
static Cupboard t3 = new Cupboard();
}

class Table{
static Bow1 b1 = new Bow1(1);
Table(){
System.out.println("Table()");
b2.f(1);
}
void f2(int marker){
System.out.println("f2("+marker+")");
}
static Bow1 b2 = new Bow1(2);
}
class Cupboard{
Bow1 b3 = new Bow1(3);
static Bow1 b4 = new Bow1(4);
Cupboard(){
System.out.println("Cupboard()");
b4.f(2);
}
void f3(int

java中类加载的时候就会把静态的变量、方法、代码块执行
Jingtai在加载的时候会执行
static Table t2 = new Table(); //1
static Cupboard t3 = new Cupboard(); //2
这两句
执行1的时候再执行 static Bow1 b1 = new Bow1(1);
static Bow1 b2 = new Bow1(2);
然后就会输出Bow1(1)
Bow1(2)
。。。。。。。。。。。。。。。。以下省略

用static声明的为静态块,而一般静态块要优先执行。