Java Initialization blocks…why should we ever use them?

Coming from a C# world I’m trying to get a grasp on the Java language, mostly for comparison purposes and to see if really the grass is greener on the other side of the fence.

What I like so far of the Java 1.6 that I see C# might benefit from:
1. The main one is cross platform portability. I wish Mono would have more support.
2. Enums are more complex, but I’m wondering if we ever really use that complexity…
3. Static imports, but do they really add to maintainability or make it worse?

What I find confusing in Java vs C#:

1. Checked exceptions: In Java, the exceptions that a method throws must be declared and are part of a method’s public interface. They were left out from C# on purpose. Here’s an interview made by Bruce Eckel (Thinking in C++) to Anders Hejlsberg (lead C# architect and Turbo Pascal creator, I should point out I wrote my first program in Turbo Pascal :-p)…
2. Initialization blocks, not the static ones that are similar to c# static constructors but instance initialization blocks…why would you ever use them? I’m still puzzled…

One thought on “Java Initialization blocks…why should we ever use them?”

  1. Initialization blocks are used if you want to initialize an instance variable lets say a list, such that value does not depend on what comes as a parameter to constructor and should be initialized whenever any constructor runs. You could mostly use static variable also in this case, but static variables have a different lifecycle for entire program run and can not be garbage collected, hence consuming memory.

Leave a Reply

Your email address will not be published. Required fields are marked *