0

I found that sometimes, if not always, static final fields will be inlined at the bytecode level. For some reason, I don't like this to happen. How do I switch this compile option off? Especially within Eclipse 4. (Without touching the source code.)

Thanks

huoenter
  • 512
  • 3
  • 16

1 Answers1

1

I don't know of a compile-time options.

Puzzle 93: Class Warfare, in the Java Puzzlers book discusses this. There can be bugs produced since null does not get "inlined".

There's a workaround to refer to them via a do-nothing function, e.g.:

public class Words {

    private Words() { }; // Uninstantiable
    public static final String FIRST  = ident("the");
    public static final String SECOND = ident(null);
    public static final String THIRD  = ident("set");

    private static String ident(String s) {
       return s;
    }

}

Obviously, this requires modifying your source code extensively. Not what you wanted...

Either buy the book, or see here for a more complete discussion

user949300
  • 15,364
  • 7
  • 35
  • 66