3

My projects had some developer who loved a static initialization block. What is the alternative to this? What is the downside of this alternative?

public class BlockTest {
    String test = new String();

    static{
        test = "test string";
    }
}

As far as I understood the static initialization block is used to set values of static field if it cannot be done in one line. But I do not understand why we need a special block for that. This leads to less readability and some confusion.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Dimitri Dewaele
  • 10,311
  • 21
  • 80
  • 127
  • 5
    Does the sample code even compile? It shouldn't because you can't access a non-static field from a static block. – Nick Holt Aug 07 '13 at 07:30
  • Im not sure I agree that static blocks are confusing. And to suggest an alternative for your situation we'd need to know exactly the difficulty with static initialiser blocks for you. You pretty much state why we need them earlier in the post; when initialisation cannot be done in one line – Richard Tingle Aug 07 '13 at 07:31
  • 2
    I think [Jon Skeet's answer](http://stackoverflow.com/a/2420405/2095090) to [this question](http://stackoverflow.com/q/2420389/2095090) explains it quite well. – Vincent van der Weele Aug 07 '13 at 07:33
  • For the quoted code of course a static initialiser us pointless – Richard Tingle Aug 07 '13 at 07:35

5 Answers5

3

The example is not good. First of all it does not compile, you cannot assign a instance variable from static init block. But if even it were correct

public class BlockTest {
    static String test = new String();

    static{
        test = "test string";
    }

it would make no sense since it is equivalent to

public class BlockTest {
    static String test = "test string";

but this static init block has no alternative

public class Object {

    private static native void registerNatives();
    static {
        registerNatives();
    }
...
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
2

It can be used for performing all the tasks that needs to be done when the class is referred for the first time, even before the instances of the class are created. It could have call to different methods or just initialization of static members. Static block ensures that these activities will be performed only once in the lifetime of the class and will be performed before any other operation takes place with regard to the class.
Programmer can depend on static block as it is ensured that the block will be executed only once and before any other activity related to that class is performed.

Moreover, I do not think it hampers readability at all. It again may vary from person to person.

Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
1

If you have static members in your class which require a longer handling, you won't get around a static initializer (constructor). These must be initialized somewhere after all. You could do that in the constructor of your class, but then you would reinitialize these values EVERYTIME you create a new object.
There is no real alternative if you must handle more than just a simple initialization.
See this post and this.

Community
  • 1
  • 1
bash.d
  • 13,029
  • 3
  • 29
  • 42
0

If you have simple assignments, you can do the assignment directly in the member declaration. No need for a separate block which just extends the complexity and readabillity.

An alternative would be to use a lazy initialization. Advantage is that it can also be arbitrary complex, but is only executed when actually needed. But of course this only works if you have getters in your classes. If you access the members directly, then this would be a big change.

Devolus
  • 21,661
  • 13
  • 66
  • 113
0

IMHO,There is no need for static block.

  String test = "test string";

And From docs

Instance variables can be initialized in constructors, where error handling or other logic can be used. To provide the same capability for class variables, the Java programming language includes static initialization blocks.

But

Note: It is not necessary to declare fields at the beginning of the class definition, although this is the most common practice. It is only necessary that they be declared and initialized before they are used.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307