`
shifulong
  • 浏览: 59174 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java静态块的一个小坑

    博客分类:
  • java
 
阅读更多
public class Student {

	private static List<Student> studentList = new ArrayList<Student>();

	static {
		studentList.add(new Student(Student.S_NAME, Student.NAME, Student.AGE));
		studentList.add(new Student(Student.S_NAME, Student.NAME, Student.AGE));
	}

	private final static String S_NAME = "aaa"; // String pool.
	private final static String NAME = new String("bbb");
	private final static Integer AGE = 11;

	private String sname;
	private String name;
	private Integer age;

	public Student(String sname, String name, Integer age) {
		this.sname = sname;
		this.name = name;
		this.age = age;
	}

	public static void main(String[] args) {
		System.out.println(studentList);
	}

	@Override
	public String toString() {
		return "Student [sname=" + sname + ", name=" + name + ", age=" + age
				+ "]";
	}
}

下面是输出:
[Student [sname=aaa, name=null, age=null], Student [sname=aaa, name=null, age=null]]

按照以前的理解,不应该是这个结果的。

于是乎问了下大神,得出了以下结论:

1,静态变量声明和初始化是两个过程;

2, String字面常量有个pool, 每次有新的常量出现的时候, 先把它放入pool, 然后直接把变量引用指向pool中的位置;

3. java中的字面常量在编译的时候被替换成真实的值,具体表现为字符串常量和基本类型常量。

4.static 块java language specification是保证happen before的,也就是有顺序的。

所以代码等同于:

	private static List<Student> studentList = null;
	
	private final static String S_NAME = "aaa"; // String pool.
	private final static String NAME = null;
	private final static Integer AGE = null;
	static {
		studentList = new ArrayList<Student>();
		studentList.add(new Student(Student.S_NAME, Student.NAME, Student.AGE));
		studentList.add(new Student(Student.S_NAME, Student.NAME, Student.AGE));
		NAME = new String("bbb");
		AGE = 11;
	 }


所以输出结果可定不对啊!!!
改成下面这样就可以了,其实就是换一下声明顺序。
	private static List<Student> studentList = new ArrayList<Student>();
	
	private final static String S_NAME = "aaa"; // String pool.
	private final static String NAME = new String("bbb");
	private final static Integer AGE = 11;
	
	static {
		studentList.add(new Student(Student.S_NAME, Student.NAME, Student.AGE));
		studentList.add(new Student(Student.S_NAME, Student.NAME, Student.AGE));
	}


输出结果
[Student [sname=aaa, name=bbb, age=11], Student [sname=aaa, name=bbb, age=11]]


自身引用自身的静态变量时才会发生上面个的情况(Student引用Student的static),
如果Student引用Teacher的静态常量就不会有上面这个问题,因为Teacher已经初始化完成所有的静态常量了。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics