java 静态块

Posted by ZhengYang on 2016-09-15

代码块 构造块 静态(代码)块

1 代码块

  • 在方法或语句中出现的{},代码块和一般的语句,顺序执行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class CodeBlock1 {
public static void main(String[] args){
{
System.out.println("代码块1"); // 1
}
System.out.println("普通语句"); // 2
{
System.out.println("代码块2"); // 3
}
}
}
//// 输出
//代码块1
//普通语句
//代码块2

2 构造块

  • 直接在类中定义且没有加static关键字的{}。
  • 构造块在创建对象时被调用,每次创建对象都会被调用,并且构造块的执行次序优先于类构造函数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class CodeBlock2 {
{
System.out.println("构造块1"); // 1 4
}
public CodeBlock2(){
System.out.println("构造函数"); // 3 6
}
{
System.out.println("构造块2"); // 2 5
}
public static void main(String[] args){
new CodeBlock2(); // 1 2 3
new CodeBlock2(); // 4 5 6
}
}
////输出
//构造块1
//构造块2
//构造函数
//构造块1
//构造块2
//构造函数

3 静态(代码)块

  • static关键字声明的代码块,静态块用于初始化类的属性。
  • 每个静态代码块只会执行一次,静态代码块先于主方法执行。
  • 如果类中包含多个静态代码块,那么将按照顺序执行静态块。
  • 静态代码块不能存在于任何方法体内。
  • 静态代码块不能直接访问静态实例变量和实例方法,需要通过类的实例对象来访问。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class CodeBlock3{
{
System.out.println("构造块"); // 2 4
}
static {
System.out.println("静态块"); // 1
}
public CodeBlock3(){
System.out.println("构造函数"); // 3 5
}
public static void main(String[] args){
new CodeBlock3();
new CodeBlock3();
}
}
//// 输出
//静态块
//构造块
//构造函数
//构造块
//构造函数

4 例一

  • 类被加载时,静态块会被执行一次。
  • new,构造块和构造函数会执行。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class CodeBlock4{
{
System.out.println("构造块"); // 7 9
}
static {
System.out.println("静态块"); // 1
}
public CodeBlock4(){
System.out.println("构造函数"); // 8 10
}
public static void main(String[] args){
new Code(); // 2 3 4
new Code(); // 5 6
new CodeBlock4(); // 7 8
new CodeBlock4(); // 9 10
}
}
class Code{
{
System.out.println("Code 构造块"); // 3 5
}
static {
System.out.println("Code 静态块"); // 2
}
public Code(){
System.out.println("Code 构造函数"); // 4 6
}
}
//// 输出
//静态块
//Code 静态块
//Code 构造块
//Code 构造函数
//Code 构造块
//Code 构造函数
//构造块
//构造函数
//构造块
//构造函数

5 例二

  • 继承会先执行父类的静态块,再执行子类的静态块。
  • new,先执行父类的构造块和构造函数,再执行子类的构造块和构造函数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class CodeBlock5 extends SuperCode{
{
System.out.println("构造块"); // 9 13
}
static {
System.out.println("静态块"); // 2
}
public CodeBlock5(){
System.out.println("构造函数"); // 10 14
}
public static void main(String[] args){
new SuperCode(); // 3 4
new SuperCode(); // 5 6
new CodeBlock5(); // 7 8 9 10
new CodeBlock5(); // 11 12 13 14
}
}
class SuperCode{
{
System.out.println("SuperCode 构造块"); // 3 5 7 11
}
static {
System.out.println("SuperCode 静态块"); // 1
}
public SuperCode(){
System.out.println("SuperCode 构造函数"); // 4 6 8 12
}
}
//// 输出
//SuperCode 静态块
//静态块
//SuperCode 构造块
//SuperCode 构造函数
//SuperCode 构造块
//SuperCode 构造函数
//SuperCode 构造块
//SuperCode 构造函数
//构造块
//构造函数
//SuperCode 构造块
//SuperCode 构造函数
//构造块
//构造函数

Reference

http://www.cnblogs.com/sophine/p/3531282.html