java初始化顺序
/*数据成员的初始化过程*/
public class InitSequence{
Tools ts1=new Tools()
{
System.out.println("Hello");
}
InitSequence(){
System.out.println("InitSequence()");
}
public static void mian(String[] args){
System.out.println("call Tools.t4.f(4) in main()");
Tools.t4.f(4);
System.out.println("Creating new Tools() in main()");
new Tools;
System.out.println("Creating new InitSequence() in main()");
new InitSequence();
}
static Tools ts2=new Tools();
}
class Tools{
Tool t1=new Tool(1);
static Tool t3=new Tool(3);
static{
System.out.println("进入静态初始化快");
t3=new Tool(33);
t4=new Tool(44);
System.out.println("退出静态初始化块");
}
{
System.out.println("进入实例初始化快");
t1=new Tool(11);
t2=new Tool(22);
System.out.println("退出实例初始化快");
}
Tools(){
System.out.println("Tools()");
t2=new Tool(222);
}
static Tool t4=new Tool(4);
Tool t2=new Tool(2);
}
class Tool{
Tool(int i){
System.out.println("Tool("+i+")");
}
void f(int i){
System.out.println("f("+i+")");
}
}
发现不了语法错误,可是编译就是通不过。。。。
------解决方案--------------------
/*数据成员的初始化过程*/
public class InitSequence{
Tools ts1=new Tools();//你少个;
public static void mian(String[] args){
System.out.println("call Tools.t4.f(4) in main()");
Tools.t4.f(4);
System.out.println("Creating new Tools() in main()");
new Tools;//new Tools();
System.out.println("Creating new InitSequence() in main()");
new InitSequence();
}