learning new things
learning new things

Reputation: 21

Java: Initialization with class declaration

I have this code and the output is different from expected. Test t1 is already being overwritten but still keeping its initial value. Why is that happening?

public class Test2 {
        Test t1 = new Test(1);

        Test2(int i) {
            t1 = new Test(i);
        }

    public static void main(String[] args) {    
         Test2 t2 = new Test2(5);
         Test2 t3 = new Test2(15);
    }
}
class Test {
    Test(int x) {
        System.out.println("Constructor called " + x);
    }
}

Output is:

Constructor called 1
Constructor called 5
Constructor called 1
Constructor called 15

Upvotes: 0

Views: 66

Answers (4)

user3437460
user3437460

Reputation: 17474

You get those output because the order of execution goes like this:

class Test2{
    Test t1 = new Test1(1);     //Executed first

    public Test2(int i){
        t1 = new Test(i);    //Executed second    
    }
}

The next thing you need to know is constructor will be invoked when you use the new keyword. Hence the constructor in class Test1 is invoked 4 times:

public static void main(String[] args) {    
     Test2 t2 = new Test2(5);    //Test1 constructor invoked twice here
     Test2 t3 = new Test2(15);   //Test1 constructor invoked twice here
}

Running Test2 t2 = new Test2(5); will invoke

new Test1(1);    //from: Test t1 = new Test1(1);
new Test(5);     //from: t1 = new Test(i);

Running Test2 t3 = new Test2(15); will invoke

new Test1(1);    //from: Test t1 = new Test1(1);
new Test(15);    //from: t1 = new Test(i);

Upvotes: 0

Zenith
Zenith

Reputation: 1205

Your question Why is that happening?

Because every time you initialize Test2, its initialize Test twice. Because each time a class load, it loads with its all child. So whenever Test2 class loads it loads and initialize the Test outside and inside of constructor.

That is why you are getting Constructor called 1 also.

Upvotes: 0

icarumbas
icarumbas

Reputation: 1815

 public class Test2 {
        Test t1 = new Test(1); // you are creating Test class twice with argument 1 so output will be 1.

        Test2(int i) {
            t1 = new Test(i); // i = 5 and 15 
        }

    public static void main(String[] args) {    
         Test2 t2 = new Test2(5);   // Here you create 2 new instances so constructor called twice
         Test2 t3 = new Test2(15);
    }
}
class Test {
    Test(int x) {
        System.out.println("Constructor called " + x);
    }
}

Upvotes: 1

Alexey Romanov
Alexey Romanov

Reputation: 170909

You seem to expect your code to be equivalent to

public class Test2 {
        Test t1;

        Test2(int i) {
            t1 = new Test(i);
        }
    ...
}

Actually, it's equivalent to

public class Test2 {
        Test t1;

        Test2(int i) {
            t1 = new Test(1);
            t1 = new Test(i);
        }
    ...
}

The code in the constructor doesn't replace default initialization; instead, initializers run before the constructor.

Upvotes: 2

Related Questions