Does Java execute sequentially within the same thread?


my code is as follows:

SomeType first = null;
if(true){
   first = new SomeType();
}
first.setSomething();

In this case, I will get NullPointerExceptionthe last line at runtime, but NOT when debugging. The execution of the code after the if-statement seems to continue, although the if-statement is not fully processed. Everything works at runtime when I change the code to:

SomeType first = new SomeType();
first.setSomething();

It does not seem that code execution at runtime aligns with the expected order at compile time. In the above case, I always expect the "first" to be created before the ".setSomething ()" is called?

+4
source share

No one has answered this question yet.

Source: https://habr.com/ru/post/1677344/


All Articles