Change private static final field using Java reflection

前端 未结 12 2608
天涯浪人
天涯浪人 2020-11-21 05:52

I have a class with a private static final field that, unfortunately, I need to change it at run-time.

Using reflection I get this error: java.lan

12条回答
  •  轮回少年
    2020-11-21 06:13

    The whole point of a final field is that it cannot be reassigned once set. The JVM uses this guarentee to maintain consistency in various places (eg inner classes referencing outer variables). So no. Being able to do so would break the JVM!

    The solution is not to declare it final in the first place.

提交回复
热议问题