public class TestRefTest { public static void main(String[] args) { TestRef c1=new TestRef(); TestRef c2=new TestRef(); c1.numero=3; c1.texto="c1"; c2.numero=30; c2.texto="c2"; System.out.println(c1.toString()); System.out.println(c2.toString()); TestRef c3=c1; System.out.println(c3.toString()); c3.texto="c3"; System.out.println(c1.toString()); System.out.println(c1==c3); c3.cambiar2(c1); System.out.println(c1.toString()); c3.texto="c2"; System.out.println(c1==c3); //Destruir objetos c1=null; c3=null; for(int i=0;i<10;i++){ System.out.println(i); } } }
La clase:
public class TestRef { public int numero; public String texto; public String toString(){ return numero+"|"+texto; } public void cambiar(TestRef c){ c=null; } public void cambiar2(TestRef c){ c.texto="CAMBIADO"; } }