Does Java execute code sequentially within a single thread?

Viewed 184


my code looks like the following:

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

In this case I will receive a NullPointerException at the last line at runtime, but NOT when debugging. It seems that code execution after the if-statement continues, although the if-statement is not completely processed. Everything works at runtime when I change the code to:

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

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

0 Answers
Related