I'm studying for an exam, and I'm looking through a sample program and I am confused. Here is the code:
public class Problem1 {
public static void f(A X)
{
A Y = X;
Y.key = X.key + 1;
}
public static void f(B X)
{
B Y = new B();
Y.key = X.key + 2; //Y.key = 12
X = Y; //X points to Y?
}
public static void main(String[] args)
{
A P = new A();
P.key = 3;
B Q = new B();
Q.key = 10;
f(P);
System.out.println(P.key);
f(Q);
System.out.println(Q.key);
P = Q;
f(P);
System.out.println(P.key);
}
}
class A
{
public int key;
}
class B extends A
{
}
I am fine with f(P). The question I have is with f(Q). I get that a new B named Y is made, and it's key is 12. The question I have is that, shouldn't X = Y point X to Y? Making Q's key value 12 rather than 10? The code prints out 4,10,11. I'm confused as to why it prints 10 rather than 12.