I suppose this code prints 512, but it prints 100.
I think f = new Foo(512);
change the reference of f from new Foo(100) to new Foo(512); then int num should be initialized as 512. But it doesn't.
Is there any my misunderstanding regarding this code?
class Foo{
private int num;
public Foo(int n){
num =n;
}
public void setNum(int n){
num = n;
}
public int getNum(){
return num;
}
}
public class Bar{
static void changeFoo(Foo f){
f = new Foo(512);
}
public static void main(String[] args){
Foo f = new Foo(100);
changeFoo(f);
System.out.println("f is " + f.getNum());
}
}