reset password
Author Message
rabbott
Posts: 1649
Posted 21:34 Oct 09, 2013 |
Anonymous said
I  don't understand why the answer for the circle question is not 3. 
As it showed in the question, the initial value for c2 = c1 which was  3. 
However, in the next line the value for c1 changed after we defined c2. 
I think I got confused because the last question was similar to this one.
 
The question asked for the output produced by the following code.

Circle c1 = new Circle(3);
Circle c2 = c1;
c1.setRadius(4);
System.out.println(c2.getRadius());

The line  Circle c2 = c1; sets the variables c1 and c2 to refer to the same circle. At this point that circle can be manipulated by using either of the two variables. It's something like the following. 

Imagine that you deposited $100 to your checking account using an ATM. You then went to a different ATM and asked for the balance in your account. The balance would include the $100 you just deposited even though you referred to the account from two different ATMs. 

The line c1.setRadius(4); sets the radius of that circle to be 4. 

The line System.out.println(c2.getRadius()); prints out the radius of that same circle. C1 and C2 are two ways of referring to a single object.

In the last question, luckyNumber and luckyNumber2 do not refer to objects. They store integer values. The difference has to do with how you refer to objects vs. how you refer to numbers.

 
Last edited by rabbott at 21:35 Oct 09, 2013.
Anonymous
Posts: 166
Posted 22:21 Oct 09, 2013 |

Thank you very much. It is obvious right now.