It doesn't have to be called in a static initializer block; it can be called in an instance initializer too, which looks just like a static initializer block but without the word static
:
public class MyClass {
staticFunction(); // Not allowed!
{
staticFunction(); // Allowed.
}
private static int staticFunction() {
return 1;
}
}
The instance initializer is called any time you create a new MyClass
object. (It's usually clearer if you put something like that in a constructor, which has about the same effect. But instance initializers can be useful for anonymous classes where you can't write your own constructor.)