static block vs private static method for static member initialization

Viewed 2190

A static variables can be initialized with the private static methods or with a static block. Are there any subtle difference between the two? Are there any situation where I cannot use the static method for initializing static members? I found the later more readable.

Static Block initialization :

private static int NUM_ITER;
static {
    // Operations
    NUM_ITER = //val from above operations.
}

Private static method initialization:

private static int NUM_ITER = calculateNumIter();

// Some method comment on how we are calculating.
private static int calculateNumIter()
{
    // Operations.
    return //value_from_operations.
}

I prefer the second one since it is more readable. Are there any situation I have to use only first (static blocks) ?

What is the best coding convention/design for initializing static members(final as well as variable)? Even from this thread I learned private static methods have an advantage over the static blocks.

thanks,

3 Answers
Related