非静态初始化,每创建Mugs对象
{
mug1=new Mug(1);
mug2=new Mug(2);
System.out.println("mug1 && mug2 init");
}结构体都会被执行一次。
package com.test.init;
public class Mugs {
public static void main(String[] args) {
System.out.println("Inside main()");
new Mugs();
System.out.println("Mugs() end");
new Mugs(1);
System.out.println("Mugs(int) end");
}
Mug mug1;
Mug mug2;
{
mug1=new Mug(1);
mug2=new Mug(2);
System.out.println("mug1 && mug2 init");
}
Mugs(){
System.out.println("Mugs()");
}
Mugs(int i){
System.out.println("Mugs(int)");
}
}
class Mug{
Mug(int marker){
System.out.println("Cup " +marker);
}
void f(int marker) {
System.out.println("f "+marker);
}
}
结果显示
Inside main()
Cup 1
Cup 2
mug1 && mug2 init
Mugs()
Mugs() end
Cup 1
Cup 2
mug1 && mug2 init
Mugs(int)
Mugs(int) end