开发者

Initialization block within a constructor

I know that an initialization block runs after the call to 'super()' in a constructor. However, when looking through some code this morning, I found the following:

public class SimpleListLocalsAnalysis extends BackwardFlowAnalysis
    FlowSet emptySet;

    public SimpleLiveLocalsAnalysis(UnitGraph graph) {
        super(graph);

        {
            Chain locals = g.getBody().getLocals();
            FlowUniverse localUniverse = new FlowUniverse(locals.toArray());
            emptySet = new ArrayPackedSet(localUniverse);
        }

        doAnalysis();
    }
...
}

The above code shows some initialisation going on within an initialisation block just after 'super(graph)' invocation. What's the purpose of placing the co开发者_开发知识库de in an initialisation block within a constructor, as surely it runs anyway after the call to super. Am I missing something here?


Its not initilization block, its simple block

just like

public void foo(){

  {
      //some code
  }
}

Purpose:

You can have restricted scope


The best way to find out is probably to ask the author of the code. Maybe he surrounded the block of code to indicate the importance of those initialization. Or maybe he did that because he wants to show that locals and localUniverse are only used for initializing emptySet.

On the other hand, in Java you can do something like

public class SomeClass extend ParenClass{
    private int val;   

    {
        //initializztion block
        val = -1;
    }

    public SomeClass()
    {
        super();
    }

    public SomeClass(String iniName)
    {
        super(iniName);
    }   
}

The initialization block will get copy to the beginning of each of the constructors (after the super call) during compile time.

So maybe the author copy and pasted the block of code into wrong place; he copied it into the constructor instead of outside of the constructor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜