リフレクションを使ってフィールドの値を変える裏技があることを知り、やってstatic final
みようと思いましたLong
Field field = Long.class.getField("MIN_VALUE");
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, 2);
System.out.println(Long.MIN_VALUE);
ただし、上記のコードは例外をスローすることも、の値を変更することもありませんLong.MIN_VALUE
。なぜそうなのですか?