2

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 code in an initialisation block within a constructor, as surely it runs anyway after the call to super. Am I missing something here?

2 Answers 2

6

Its not initilization block, its simple block

just like

public void foo(){

  {
      //some code
  }
}

Purpose:

You can have restricted scope

Sign up to request clarification or add additional context in comments.

4 Comments

So what's the purpose of a 'simple block'? Why use it?
You can have advantage of scope.. you can do some local stuff in this block.
Ok, to ensure that whatever you define is not accessible outside of the block? Is this why you would use them?
Makes sense. I've never seen them before strangely enough. Thanks for the clarification.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.