Could anyone explain how Java executes this code? I mean the order of executing each statement.
public class Foo
{
boolean flag = sFlag;
static Foo foo = new Foo();
static boolean sFlag = true;
public static void main(String[] args)
{
System.out.println(foo.flag);
}
}
OUTPUT:
false
foois null andsFlagis falsefoo) runs:Foois createdflagexecutes – currentlysFlagis false, so the value offlagis falsesFlag) executes, setting the value to truemainruns, printing outfoo.flag, which is falseNote that if
sFlagwere declared to befinalit would be treated as a compile-time constant, at which point all references to it would basically be inlined totrue, sofoo.flagwould be true too.