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
Test t1 = new Test(1);should do then? What makes you think so?