I need your knowledge to explain something very simple that confuses me
As you can see, it's a very beginning practice on Java, and I already face the first confusions.
So the question is, what is true from the A, B, C, D?
A. line 12 prints 4
B. line 13 prints 9
C. line 13 prints 18
D. line 14 prints 18
I know that the answers are C and D but because I'm studying and I try to understand why, could you please explain this to me?
I first thought that the correct ones whas A and B but it comes out that I was wrong.
What is really going on with ob.t = ob2; ob2.t = ob;
and whats the role of Test t;
in the Class Test
?
1 class Test {
2 Test t;
3 static int a;
4 Test(int i) { a = i; }
5 void xchange(Test ob, int i) { ob.a = i * ob.a; }
6 }
7 class Call {
8 public static void main(String args[]) {
9 Test ob = new Test(2); Test ob2 = new Test(3);
10 ob.t = ob2; ob2.t = ob;
11 ob.xchange(ob, 2); ob2.xchange(ob.t, 3);
12 System.out.println(ob.a);
13 System.out.println(ob2.a);
14 System.out.println(ob.t.a);
15 }
16 }