Creating an object in a static way

前端 未结 5 1232
Happy的楠姐
Happy的楠姐 2020-12-22 23:52

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         


        
5条回答
  •  旧巷少年郎
    2020-12-23 00:06

    at first static fields should run and at first inline! so at first line 4 and then 5 will run so foo is initialized first and as we know boolean variables are initialized to false by default so at first so as the foo is initialized the field of flag is sflag that's false and then sfalsg becomes true that won't change flag( there's no relation) then at last main runs and print falg that is false!!! I hope to be useful! Be successful

提交回复
热议问题