0

As an alternative to static blocks, Oracle docs are suggesting to call a method(s) and the example is using a variable assignment:

public static varType myVar = initializeClassVariable();

The advantage of private static methods is that they can be reused later if you need to reinitialize the class variable.

But if I don't need (and want to avoid unused) extra variable and also return statement in my static block, what is a better way to call static code?

Calling a static method in the constructor is wrong design for executing static code once (constructor can be private for utility class) for static block

public MyClass() {
     MyClass.initializeClassVariable();
}

So is the only improvement is reducing variable access level to private ?

 private static varType myVar = initializeClassVariable();

Or a better approach is to keep static block and add the method there?

static {
    initializeClassVariable();
}
Community
  • 1
  • 1
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • What do you mean by *"I don't need (and want to avoid unused) extra variable"*? What **extra** variable are you talking about? Isn't the point of the static code to initialize a static field, i.e. the field is there either way? – Andreas Oct 08 '18 at 05:03
  • @Andreas I don't need `myVar`, it's just defined for executing static code – Ori Marko Oct 08 '18 at 05:03
  • 1
    Then what does the static code do, if not initializing static fields? The "alternative to static blocks" is about an alternate way of initializing static fields. – Andreas Oct 08 '18 at 05:04
  • If you use a static block, you don't call the method again through there. You run the code that would have been in the method there as a one-time operation. – faris Oct 08 '18 at 05:07
  • If the static block is not initializing a static field, or is complex logic initializing multiple static fields at the same time, then the "alternative" is not appropriate. The documentation simply mentions that there *is* an alternative, not that it is always applicable, or that you must use it. – Andreas Oct 08 '18 at 05:08
  • @Andreas I can update several static variables which may not be in class – Ori Marko Oct 08 '18 at 05:11
  • Then the alternative is not appropriate. But why is the static code updating static fields in another class? Shouldn't the code be in the other class then? – Andreas Oct 08 '18 at 05:12
  • @Andreas static fields can be public and you may enter similar logic to update them in separate class – Ori Marko Oct 08 '18 at 05:14
  • Just because you can, doesn't mean you should. But that's beside the point. The "alternative" is specifically for initializing a single static field in your own class. If that's not what you're doing, then the "alternative" is not appropriate. – Andreas Oct 08 '18 at 05:16
  • @Andreas Consider also several blocks put different objects to map. I can't assign map more than once – Ori Marko Oct 08 '18 at 05:18
  • Then combine the blocks. You never *have to* use more than one static block, though it can help with code readability. – Andreas Oct 08 '18 at 05:23

1 Answers1

1

The "alternative to static blocks" is about initializing a single static field.

Example:

class A {
    static Map<String, Integer> romans;
    static {
        romans = new HashMap<>();
        romans.put("I", 1);
        romans.put("V", 5);
        romans.put("X", 10);
    }
}

Alternative:

class A {
    static Map<String, Integer> romans = initRomans();

    private static Map<String, Integer> initRomans() {
        Map<String, Integer> r = new HashMap<>();
        r.put("I", 1);
        r.put("V", 5);
        r.put("X", 10);
        return r;
    }
}

As the article says, with this code you can reset the static field.

    public static void resetRomans() {
        romans = initRomans();
    }

If your code does something else, then the "alternative" isn't applicable, and you write the code in a static initializer block.

class A {
    static {
        Manager.register(A.class);
    }
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Another alternative is an anonymous initialization block – faris Oct 08 '18 at 05:26
  • @faris Yeah, but that's generally a bad idea, see [Efficiency of Java “Double Brace Initialization”?](https://stackoverflow.com/q/924285/5221149) It might seems cool, but creating an entire class just to initialize the field, is overkill. – Andreas Oct 08 '18 at 05:32