-1

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 }
nempoBu4
  • 6,521
  • 8
  • 35
  • 40
Sh3
  • 9
  • 1

1 Answers1

0

What's the role of Test t; in class Test?

It declares a field in Test of type "reference to Test". Note that variables and fields in Java cannot store objects; they can only store references to objects.

What is really going on with ob.t=ob2; ob2.t=ob;?

It sets the t fields of both Test objects to contain references to each other. So the first object's t field contains a reference to the second object, and vice versa.

It is tempting to say that "it assigns ob's t field to contain a reference to ob2", but strictly speaking this is incorrect, as ob and ob2 are not objects.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • So as i realise, the: ob.t has a reference to ob2 and that means that when the ob2.xchange(ob.t,3); is called, the values inside it, are: (3,3)? – Sh3 Feb 17 '15 at 03:03
  • @Sh3 the values inside what? – user253751 Feb 17 '15 at 03:06
  • when that method is called: ob2.xchange(ob.t,3); , ob.t has a reference to ob2 so, i guess that it has a value of 3? – Sh3 Feb 17 '15 at 03:12
  • @Sh3 `ob.t` doesn't have a value of 3, it has a value which is a reference to the second object. – user253751 Feb 17 '15 at 03:15
  • @Sh3 I think the point of the assignment question is that `a` is static (which is unrelated to the question you asked). If `a` was not static, A and B would be correct (unless I made a mistake somewhere) – user253751 Feb 17 '15 at 03:17
  • so why line 13 gives the result of 18? – Sh3 Feb 17 '15 at 03:23
  • Whenever you access a static field (please note that `a` is static) through a object reference, the compiler actually replace it with the class name of the reference. Therefore, because `ob` is of type `Test`, `ob.a` will become `Test.a`, which is the same case for `ob2.a` and even `ob.t.a`, they will all become `Test.a`. With such understanding, it should be straight-forward to understand why you got such result – Adrian Shum Feb 17 '15 at 03:32
  • i finally found it out. Thank you! – Sh3 Feb 17 '15 at 13:56