public class ReferTest {
class Foo{
int a;
public Foo(int a) {
this.a = a;
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
@Override
public String toString() {
return "Foo [a=" + a + "]";
}
}
private void change(Foo foo) {
foo.setA(10);
}
private void change1(Foo foo) {
Foo f = new Foo(20);
foo = f;
}
public static void main(String[] args) {
ReferTest refer = new ReferTest();
Foo f1 = refer.new Foo(1);
refer.change(f1);
System.out.println("f1 = " + f1);//输出:f1 = Foo [a=10]
Foo f2 = refer.new Foo(2);
refer.change1(f2);
System.out.println("f2 = " + f2);//输出:f2 = Foo [a=2]
char c = 'b';
refer.swap1(c);
System.out.println("char = " + c);//输出:char = b
char[] carray = {'g', 'o', 'o', 'd'};
refer.swap2(carray);
System.out.print("carray = ");
for (int i = 0; i < carray.length; i++) {
System.out.print(carray[i]);
}//输出: carray = aood
}
private void swap1(char c) {
c = 'a';
}
private void swap2(char[] carray){
carray[0] = 'a';
}
}