Last active
January 6, 2020 12:31
-
-
Save toandv/391f6fbd7b02b341b042f05b5c9fc2a9 to your computer and use it in GitHub Desktop.
ReSetFinalFields.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ReSetFinalFields { | |
static class Final { | |
public final String name; | |
public Final() { | |
this(null); | |
} | |
public Final(String name) { | |
this.name = name; | |
} | |
@Override | |
public String toString() { | |
return "Final{" + | |
"name='" + name + '\'' + | |
'}'; | |
} | |
} | |
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { | |
Final finalObject = new Final("final"); | |
System.out.println(finalObject); | |
Field fieldName = Final.class.getDeclaredField("name"); | |
fieldName.setAccessible(true); | |
// Field fieldOverride = AccessibleObject.class.getDeclaredField("override"); | |
// fieldOverride.setAccessible(true); | |
// fieldOverride.set(fieldName, true); | |
fieldName.set(finalObject, "not final"); | |
System.out.println(finalObject); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment