Cannot understand

package twister;

class Test

{

Test(int i)

{

System.out.println("Test(" +i +")");

}

}

publicclass Q12

{

public Q12()

{

System.out.println("Constructor");

}

static Test t1 =new Test(1);

Test t2 =new Test(2);

static Test t3 =new Test(3);

publicstaticvoid main(String[] args)

{

Q12 Q =new Q12();

}

}

After running this code I get output as

Test(1)

Test(3)

Test(2)

Constructor

I understand Test(1),Test(3) since they are static and will be initialised when the class loads.

But how did Test(2) come in the output.

who is calling this.

I am only creating an object for my class in my program which I guess is calling the constructor. so how did Test(2) come.

[1676 byte] By [jaaya] at [2007-10-2 20:42:26]
# 1

You've defined (non-static) a field named t2 with a initial value of "new Test(2)" in your class Q12. When you instanciate Q12 then all its fields will be initialized before the Constructor is called (simply speaking). Since initializing the field t2 requires a new instance of Test the constructor of Test will have to be called.

JoachimSauera at 2007-7-13 23:25:47 > top of Java-index,Java Essentials,New To Java...