JAVA尽可能保证所有变量在使用前都得到初始化。


一:java会对未初始化的变量进行检查

void test(){
		int i;
		i++;
	}



报错:The local variable i may not have been initialized


二:java自动初始化基本类型


public class C {
	
	class CC{
		
		CC(){
			System.out.println("INIT CC");
		}
	}
	
	C(){
		System.out.println("INIT C");
	}
	byte b;
	short s;
	int a;
	long l;
	
	double d;
	float f;
	
	boolean bl;
	
	char ch;
	
	
	public static void main(String[] args) {
		
		C c = new C();
		System.out.println(c.b);
		System.out.println(c.s);
		System.out.println(c.a);
		System.out.println(c.l);
		System.out.println(c.d);
		System.out.println(c.f);
		System.out.println(c.bl);
		System.out.println(c.ch);
		
	}

}


INIT C  

0  

0  

0  

0  

0.0  

0.0  

false

三:手动初始化


public class C {
	
	class CC{
		
		CC(){
			System.out.println("INIT CC");
		}
	}
	
	C(){
		System.out.println("INIT C");
	}
	byte b;
	short s;
	int a;
	long l;
	
	double d;
	float f;
	
	boolean bl;
	
	char ch;
	
	
	int i = genInt();
	
	int genInt(){
		System.out.println("执行了赋值");
		return 1;
	}
	
	int ii = genII(i);
	
	int genII(int i){
		return i*i;
	}
	
	public static void main(String[] args) {
		
		C c = new C();
		System.out.println(c.b);
		System.out.println(c.s);
		System.out.println(c.a);
		System.out.println(c.l);
		System.out.println(c.d);
		System.out.println(c.f);
		System.out.println(c.bl);
		System.out.println(c.ch);
		System.out.println(c.i);
		System.out.println(c.ii);
	}

}



执行了赋值

INIT C

0

0

0

0

0.0

0.0

false


赋值在构造前


四:

public class C {
	
	class CC{
		
		CC(){
			System.out.println("INIT CC");
		}
	}
	
	C(){
		i = 4;
		System.out.println("INIT C");
	}
	
	int i = genInt();
	
	int genInt(){
		System.out.println("执行了赋值");
		return 1;
	}
	
	public static void main(String[] args) {
		
		C c = new C();
		System.out.println(c.i);
	}

}

执行了赋值
INIT C
4

注:
先i=1,后i=4


五:初始化顺序

按照成员变量的生命顺序初始化。



public class C {
	
	class CC{
		int i;
		CC(int i){
			this.i = i;
			System.out.println("INIT CC "+i);
		}
		
		@Override
		public String toString() {
			// TODO Auto-generated method stub
			return "CC "+i;
		}
	}
	
	C(){
		System.out.println("INIT C");
		cc3 = new CC(33);
	}
	
	CC cc1 = new CC(1);
	
	int i = genInt();
	
	CC cc2 = new CC(2);
	
	int genInt(){
		System.out.println("执行了赋值");
		return 1;
	}
	CC cc3 = new CC(3);
	
	
	public static void main(String[] args) {
		
		C c = new C();
		System.out.println(c.i);
		System.out.println(c.cc3);
	}

}



六:静态变量只初始化一次



public class C {
	
	class CC{
		int i;
		CC(int i){
			this.i = i;
			System.out.println("INIT CC "+i);
		}
		
		@Override
		public String toString() {
			// TODO Auto-generated method stub
			return "CC "+i;
		}
	}
	
	static int o = getO();

	static int getO(){
		System.out.println("oooo");
		return 9;
	}
	
	static int getOther(){
		System.out.println("other");
		return 9;
	}
	
	
	C(){
		System.out.println("INIT C");
	}
	
	public static void main(String[] args) {
		
		C c = new C();
		C c1 = new C();
	}

}

结果:

oooo
INIT C
INIT C


注:初始化两个C,静态o只初始化一次,静态方法未调用,则不执行

七:静态变量初始化顺序


public class Hero {

	private String name;
	
	public Hero(String name) {
		this.name = name;
		System.out.println("hero :"+name);
	}
	
	public void gank(){
		System.out.println(this.name + " gank");
	}
}



public class Heros {

	static Hero h2 = new Hero("AM");
	
	static Hero h1 ;
	
	static {
		h1 = new Hero("AA");
	}
	
	public Heros() {
		System.out.println("heros");
	}
}



注:

static{

}


test 一:

public class Test {

	
	public static void main(String[] args) {
		
	}
	
	static Heros hs2 = new Heros();
	static Heros hs1 = new Heros();
}



输出:

hero :AM
hero :AA
heros
heros

注:空的main方法,但是仍需要初始化test的两个静态变量。

heros构造执行了两次,因为new了2次。

new第一次的时候heros中的静态Hero h1和h2都初始化。

new第二次的时候heros中的静态Hero h1和h2不再初始化。


test 二:

public class Test {

	
	public static void main(String[] args) {
		
		Heros.h1.gank();
	}
	
}



hero :AM


hero :AA


AA gank

注:访问heros中的静态变量h1,则h1初始化了。同时h2也初始化。

而heros本身的构造没有执行,因为没有new。

若:

    Heros.h1.gank();
        Heros.h2.gank();

h2在第一行就已经初始化,在执行 Heros.h2.gank();不需要再次初始化。


八:你见过吗?



public class Heros {

	public Hero h3;
	
	{
		h3=new Hero("BM");
		System.out.println("before init");
	}
	
	
	public Heros() {
		System.out.println("heros");
		System.out.println(h3);
	}
}



public class Test {

	public static void main(String[] args) {
		
		Heros hs = new Heros();
	}
	
}




hero :BM
before init
heros
Hero@1fb8ee3