int x;

x = 3;

int y = x;

x = 5;

y = y + x;

System.out.println(x);

System.out.println(y);

The output is:

5

8

+10 votes

This is because of the order in which java reads the code. So, y = x when x = 3. After this line x is then changed to 5; however, y is still 3 because that is what x was when y = x.

If the statement "y = x;" would have come after "x = 5;" then y would equal 5.

In other words, the statement "y = x;" in java is not the same as saying y will always be equal to x. y is merely set to whatever value x currently holds.