Does java(compiler or jvm) handles static final members of class differently? If yes how

前端 未结 3 1903
眼角桃花
眼角桃花 2021-01-21 03:11

It seems logical to do some optimization around static final constants ( e.g. replace the variable with literals etc ) to improve the performance

3条回答
  •  梦毁少年i
    2021-01-21 03:19

    Yes, javac already does this (assuming your static final fields are for a primitive type or String): the constant value is "pasted in" directly at the point of use.

    Of course, the downside to this is that if you change the field and forget to recompile the classes that use that field, then the value will be stale....

提交回复
热议问题