Could anyone explain how Java executes this code? I mean the order of executing each statement.
public class Foo
{
boolean flag = sFlag;
static Foo f
foo
is null and sFlag
is falsefoo
) runs:
Foo
is createdflag
executes - currently sFlag
is false, so the value of flag
is falsesFlag
) executes, setting the value to truemain
runs, printing out foo.flag
, which is falseNote that if sFlag
were declared to be final
it would be treated as a compile-time constant, at which point all references to it would basically be inlined to true
, so foo.flag
would be true too.