¿Por qué este lanzamiento de Java arroja un error?
Me preguntaba por qué hacer referencia a "w" despuésobj = w;
arrojará un error. ¿No estás creando otro puntero a esa instancia de w diciendo obj = w? Es decir. ¿Por qué es diferente decir algo comoString s = "hi"; String w = s;
¡Gracias!
public class Casting {
public static void main(String[] args) {
// casting doesn't change the object
Object obj;
{
Stopwatch w = new Stopwatch();
obj = w;
}
System.out.println(obj); // this line does work
System.out.println(w); //this line does not work
}
}