What is the alternative to a non-static initialization block?

耗尽温柔 提交于 2019-12-05 04:21:34

First of all, it doesn't make sense to initialize test to a new String() there, since the initialization block immediately assigns it to something else. Anyways...

One alternative is initializing at the declaration:

public class BlockTest {
    String test = "testString";
}

Another is in the constructor:

public class BlockTest {
    String test;
    public BlockTest () {
        test = "testString";
    }
}

Those are the two main, common options.

There are two main uses for an initialization block. The first is for anonymous classes that have to perform some logic during initialization:

new BaseClass () {
    List<String> strings = new ArrayList<String>();
    {
        strings.add("first");
        strings.add("second");
    }
}

The second is for common initialization that must happen before every constructor:

public class MediocreExample {
    List<String> strings = new ArrayList<String>();
    {
        strings.add("first");
        strings.add("second");
    }
    public MediocreExample () {
        ...
    }
    public MediocreExample (int parameter) {
        ...
    }
}        

However, in both cases there are alternatives that do not use the initialization block:

new BaseClass () {
    List<String> strings = createInitialList();
    private List<String> createInitialList () {
        List<String> a = new ArrayList<String>();
        a.add("first");
        a.add("second");
        return a;
    }
}

And:

public class MediocreExample {
    List<String> strings;
    private void initialize () {
        strings = new List<String>();
        strings.add("first");
        strings.add("second");
    }
    public MediocreExample () {
        initialize();
        ...
    }
    public MediocreExample (int parameter) {
        initialize();
        ...
    }
}        

There are many ways to do these things, use the way that is most appropriate and provides the clearest and most easily maintainable code.

Compiler inserts non-static init block into each constructor. The code needed to initialize instance fields is also inserted into each constructor. This

class Test1 {
    int x = 1;
    {
        x = 2;
    }
    Test1() {
        x = 3;
    }
}

compiles into the same bytecoode as this

class Test1 {
    int x;
    Test1() {
        x = 1;
        x = 2;
        x = 3;
    }
}

initiaization block has no alternative, in fact its an alternative to constructor.

public TestClass {
        TestClass() {

        }
    }

this is useful in case of anonymous class, because you can't have a constructor in that, reason is pretty straight forward, that you don't have name for the class, then you can't have constructor otherwise what will you name it.

new MyClass(){
   //its an anonymous class, you can't use constructor here
      {
      }
  }

however you can initialize the variable inline with your declaration like

public TestClass {
    String test = "value";
}

but it is not an alternative, because you cant perform any operation (say arithmetic or string operations) this way, but you can do in initializer block

public TestClass {
        String test = "value";
        test = test + " not found"//compiler error
        {
         test = test + " not found" // valid
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!