3

Possible Duplicate:
Static Initialization Blocks

While reviewing some old code, I found some rather odd syntax that I've never seen before. After doing some research, I know now that what I saw was a static{} block which (if I understand correctly) is a block of code that is executed at initialization time.

What I don't understand is the advantage to having such a feature or when one would possibly want to use this. It seems like the static blocks simply contain regular lines of code which could belong in any regular static method, so what is the advantage to having it run automatically at initialization (other than saving a programmer a line of code to call a method)? Why is this important or useful?

Community
  • 1
  • 1
asteri
  • 11,402
  • 13
  • 60
  • 84

4 Answers4

4

As they say in the Java Tutorials:

If initialization requires some logic (for example, error handling or a for loop to fill a complex array), simple assignment is inadequate. 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.

Chris B
  • 925
  • 4
  • 14
3

As you said, static blocs are executed at initialization time. Say that you have a static field which can be pretty complex, for example

private static List<ThingsDownloadedByTheNet> ls;

just doing

private static List<ThingsDownloadedByTheNet> ls = new ArrayList<ThingsDownloadedByTheNet>

could not be enough, as you maybe want to set list elements too. So, you do a static block, in which you perform some initialization (in this case you connect to a server and populate list elements)

user1610075
  • 1,583
  • 3
  • 15
  • 32
2

Think of it as a constructor for static variables. It gives you a chance to initialize them before anyone uses them.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 4
    maybe the more important distinction is that you can initialize multiple final variables, which you can't do with a method call. – jtahlborn Sep 07 '12 at 11:37
  • @jthalborn, thanks. I didn't realize you could manipulate `final` variables in the `static{}` block. That, combined with the below answers about initializing complex object types, answers the question. – asteri Sep 07 '12 at 11:45
0

static{} block is executed when the class is loaded. You can use it to initialize static fields or call static methods. You cannot use any method to replace this behavior, since a class "has no constructor". Actually you can see this as a "class constructor".

m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • actually, you can use static methods to initialize static members. – jtahlborn Sep 07 '12 at 11:37
  • I know. What I meant is that such methods are not launched at class loading, so you would have to call them from other code if you want such code to be executed. – m0skit0 Sep 07 '12 at 11:39
  • 1
    you can call a static method to initialize a static member, e.g. `private static final MY_VAR = someStaticMethod()`. (which is executed at classloading time) – jtahlborn Sep 07 '12 at 11:44
  • Sure, but this way you need one method for each field. You cannot do several initializations. Well, you can of course, but doing such side effects are bad for code readability and maintainability. – m0skit0 Sep 07 '12 at 12:01