2

Possible Duplicate:
Static Initialization Blocks

What does the following mean in java?

static {                                        
  WritableComparator.define(IntPair.class, new Comparator());
}
Community
  • 1
  • 1
MedicineMan
  • 15,008
  • 32
  • 101
  • 146

2 Answers2

9

That means static initialization block which will be executed on class load.

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.

kosa
  • 65,990
  • 13
  • 130
  • 167
1

It means that the code within that block will run once, when the type is loaded, before any constructors are called, but after any field initialisers are run.

Note that you cannot set any instance fields in the static block. There is no concept of this in there, just as in any other static methods.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742