public class People
{
public static int num=0;//静态变量
String name;
static//静态初始化块
{
System.out.println(num+":静态初始化块");
}
{//实例初始化块
System.out.println(num+":实例初始化块");
}
public People(String name)
{
System.out.println(num+":构造函数初始化");
this.name=name;
num++;
}
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
People people=new People("lip");
People people2=new People("xiaoli");
}
}
运行效果:
0:静态初始化块
0:实例初始化块
0:构造函数初始化
1:实例初始化块
1:构造函数初始化
{
public static int num=0;//静态变量
String name;
static//静态初始化块
{
System.out.println(num+":静态初始化块");
}
{//实例初始化块
System.out.println(num+":实例初始化块");
}
public People(String name)
{
System.out.println(num+":构造函数初始化");
this.name=name;
num++;
}
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
People people=new People("lip");
People people2=new People("xiaoli");
}
}
运行效果:
0:静态初始化块
0:实例初始化块
0:构造函数初始化
1:实例初始化块
1:构造函数初始化