---
title: "Java类的初始化"
author: "yoyo08"
date: "2010-02-01T18:28:01Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2010-02-01T18:28:01Z"
id: 585249
url: "https://yoyo08.iteye.com/blog/585249"
---

如下面代码

 

```
public class Test1 
{
	public static void main(String[] args)
	{
		Test2 t2 = new Test2();
		System.out.println();
		Test3 t3 = new Test3();
	}
}

class Test2
{
	static
	{
		System.out.println("Test2 static");
	}
	
	public Test2()
	{
		System.out.println("Test2 create");
	}
}

class Test3 extends Test2
{
	static
	{
		System.out.println("Test3 static");
	}
	
	public Test3()
	{
		System.out.println("Test3 create");
	}
}
```

 

输出为：

 

```
Test2 static
Test2 create

Test3 static
Test2 create
Test3 create
```

 

 

如果把Test2 t2 = new Test2();注释掉，这时输出为：

```
Test2 static
Test3 static
Test2 create
Test3 create
```