How to create a (static) strictfp initializer block?

While refactoring some code, I came across this oddity. It seems impossible to control the strictfp property for an initializer without affecting the entire class. Example:

public class MyClass {

    public final static float[] TABLE;
    strictfp static { // this obviously doesn't compile
         TABLE = new float[...];
         // initialize table
    }

    public static float[] myMethod(float[] args) {
         // do something with table and args
         // note this methods should *not* be strictfp
    }

}

From JLS, section 8.1.1.3 I understand that the initializer will be strictfp if the class is declared using the strictfp modifier. But he also says that he does all methods implicitly strictfp:

The effect of the strictfp modifier is that all popup or double expressions inside a class declaration (including inside variable initializers, initializer initializers, static initializers, and constructors) are explicitly FP-strict (Β§15.4).

, , , , , .

, , , strictfp? strictfp, ?

, , , strictfp'dness?

+5
1

, :

  • ,
  • MyClass.myMethod ,
  • API "" ,
  • -

... :

class MyClass {
  //1) initialized/called once
  public final static float[] TABLE = MyClassInitializer.buildSomething();

  public static float[] myMethod(float[] args) {
    //2) non-strict
  }
}

//3) doesn't "pollute" the MyClass API
class MyClassInitializer {
  strictfp [static] float[] buildSomething() { //4) strictfp here or on the class
    //TODO: return something
  }
}

singleton, . , .

0

All Articles