Originally posted by Jairat:
I have this code...
class TryEx
{
int i =10;
public static void main(String args[])
{
TryExex = new TryEx();
ex.i =10;
ex.method(ex);
System.out.println(ex.i);
}
void method(TryEx exref)
{
exref.i = 20;
TryEx h = new TryEx();
h.i = 100;
exref = h;
//exref = null; (1)
}
}
The output I expected was 100. But the output is 20. How?
For that matter even when exref is assigned null as in (1), the output is 20.
If you make the instance variable i, static then it is printing 100. This is understadable, considering that the class shares the class variable with all instances and hence any changes made by any object of the class will effect the class variable.
So can anybody explain this. Thanks
Originally posted by vasansrini:
....
Here, the just the value the reference variable exref is changed [b]not the contents of the memory location whose address is in exref.
[/B]
The overall mission is to change the world. When you've done that, then you can read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|