6

Why should I use static initialization block when I can initialize static members through a constructor?

2
  • 9
    What if you never have an instance of that class - but would like to use the static functions of it, which uses the values initialized by the static initializer block? Commented Mar 6, 2013 at 11:41
  • 1
    and every time when c'tor is called, static values will be reset. bad!! Commented Mar 6, 2013 at 11:42

3 Answers 3

6

Firstly, you might never have any instances of your class. Or you might want to have the static members iniailized before you have any instances of the class.

Secondly, initializing static members from constructors is more work:

  • you'll need to make sure that every constructor does this;
  • you need to maintain a flag to keep track of whether static members have been initialized;
  • you have to think about synchronization to prevent race conditions;
  • you may have to consider performance implications of the synchronization, especially if you have many threads creating many instances of your class.

Lastly, it's usually the wrong thing to do conceptually (I say "usually" because there are legitimate uses for lazy initialization).

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

1 Comment

+1 - though if you were going to implement lazy (or non-lazy deferred) initialization, you probably wouldn't "make it happen" in a constructor.
1

A static member is not associated to any instance of the class, while the constructor creates an instance. You may use static members without having a single instance of the class, they will still have to be initialized. In this case a constructor can not do the job.

Comments

0

So why:

static Set<String> digits = new HashSet<String>();
static {
    Collections.add(digits, "unu", "du", "tri", "kvar", "kvin");
    digits.add("ses");
    digits.add("sep");
    digits.add("ok");
}

if the following is possible:

static Set<String> digits = new HashSet<String>() {{
    Collections.add(this, "unu", "du", "tri", "kvar", "kvin");
    add("ses");
    add("sep");
    add("ok");
}};
  1. It introduces a new anonymous class, a file in the jar; not so optimal.
  2. The second form is a geeky play thing.

3 Comments

why is introducing new anonymous classes not optimal? would it ever lead to classloading bugs? or are you just saying you don't want to bloat your jar.
@DavidT. I have no dislike for either form, but comparing both: the first has redundant occurrences of digits and the second creates an anymous class (tiny overhead) and has a misleading syntax for non-java developers. The real danger would be when using a non-static {{ ... }} on a Serializable class: then the this of the surrounding class get's serialized too. I have encountered such a X.this being null after reloading of the object for instance.
Actually, i found out one difference -> on Eclipse, if you get a crash inside your Anonymous class, Eclipse doesn't even know which project to point the line of crash to (like, say you have a bunch of threads and runnables as anonymous classes)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.