I was wondering why the link "w" after obj = w;throws an error. Aren't you just creating another pointer to this w example by saying obj = w? That is why it is different to say something like String s = "hi"; String w = s;Thank you!
obj = w;
String s = "hi"; String w = s;
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 } }
There is nothing like quoting JLS in the first place in the morning.
JLS 6.3. Scope of declaration:The scope of a local variable declaration in a block (§14.4) is the rest of the block in which the declaration appears, starting with its own initializer and including any other declarators to the right of the local variable declaration statement.
JLS 6.3. Scope of declaration:
The scope of a local variable declaration in a block (§14.4) is the rest of the block in which the declaration appears, starting with its own initializer and including any other declarators to the right of the local variable declaration statement.
and
JLS 14.2. Blocks:, .
JLS 14.2. Blocks:
, .
? varialbe w
w
{ Stopwatch w = new Stopwatch(); obj = w; }
( " " ), . ,
System.out.println(w);
w .
obj?
obj
public static void main(String[] args) { Object obj; { Stopwatch w = new Stopwatch(); obj = w; } System.out.println(obj); System.out.println(w); }
.
System.out.println(obj);
, obj .
w obly, . ,
public class Casting { public static void main(String[] args) { // casting doesn't change the object String w; Object obj; { w = new String(); obj = w; } System.out.println(obj); // this line does work System.out.println(w); //this line now working } }
w , . . , :
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); System.out.println(w); } }
, . , , . java , , . , - "if" "try", . . . , - -, (), (, ), .
public class Casting { public static void main(String[] args) { // casting doesn't change the object Object obj; Stopwatch w ; { w = new Stopwatch(); obj = w; } System.out.println(obj); // this line does work System.out.println(w); //this line does not work } }
Source: https://habr.com/ru/post/1622519/More articles:Counter for the selected option - javascriptзакрытие может использовать новое ключевое слово и что такое разница, например, при создании нового объекта/класса - javascriptUnable to install elementtree with pip - pythonРабота с формой редактирования внутри модального диалога bootstrap с использованием php - ajaxA local variable in Java with the same name as a global variable - javaReduce div width repeatedly - javascriptIs a unicode byte order token valid at the beginning of a UNIX script file? - linuxInteger conversion in floating point arithmetic - c ++Set cell width to 3 tables - javaCancel updating the update panel when changing an application variable - javascriptAll Articles