---
title: "SCJP习题精选2009072101"
author: "nail2008"
date: "2009-07-21T16:10:09Z"
tags: []
categories: ["编程语言"]
slug: "scjp-2009072101"
draft: false
lastmod: "2009-07-21T16:10:09Z"
id: 430159
url: "https://neilDuan.iteye.com/blog/430159"
---

Given:

```
class Bird {
	{System.out.print("b1 ");}
	public Bird(){System.out.print("b2 ");}
	static {System.out.print("b3 ");}
}
class Raptor extends Bird{
	static {System.out.print("r1 ");}
	public Raptor(){System.out.print("r2 ");}
	{System.out.print("r3 ");}
	static {System.out.print("r4 ");}
}
class Hawk extends Raptor{
	
	public static void main(String[] args){
		System.out.print("pre ");
		new Hawk();
		System.out.print("hawk ");
	}
	{System.out.print("h1 ");}
	static {System.out.print("h2 ");}
}
```

 What is the result?

 

 

 

Answer: b3 r1 r4 h2 pre b1 b2 r3 r2 h1 hawk

 

分析：此题考察“初始化块”。那么是怎样的顺序讷？

- 首次加载类时，会运行一次静态初始化块。
- 每当创建一个新的类实例时，都会运行实例初始化块。它们的运行发生在所有父构造函数之后、构造函数的代码运行之前。
- 如果一个类中存在多个初始化块，它们会遵循上述规则，并且它们的运行顺序与它们在源文件中出现的顺序相同。