2

Possible Duplicate:
Static initializer in Java

I have a few years' experience with Java, but I've recently run across something I've never seen before:

public class Project{
...
static{
  initDataTypeMapping();
}
...
}

How does this method signature work? Is this in fact even technically a method? I'm wondering why one wouldn't simply put the the method call to initDataTypeMapping() in the constructor. Just trying to increase my understanding so I don't mess something up. Thanks!

Community
  • 1
  • 1
FreakinOutMan
  • 247
  • 1
  • 2
  • 14

1 Answers1

6

This is known as a static initializer.

The code in the static { } block is run when the class is first loaded by the classloader (which is usually, but not always, when code that refers to the class is first loaded/executed), and is guaranteed to be run in a thread-safe manner.

See this question also.

Community
  • 1
  • 1
matt b
  • 138,234
  • 66
  • 282
  • 345