fork(1) download
  1. import java.lang.reflect.*;
  2.  
  3. class Foo {
  4. private final String bar;
  5.  
  6. public Foo(String bar) {
  7. this.bar = bar;
  8. }
  9.  
  10. public String getBar() {
  11. return this.bar;
  12. }
  13.  
  14. }
  15.  
  16. public class Main {
  17.  
  18. public static void main(String[] args) {
  19. Foo foo = new Foo("foobar");
  20. System.out.println(foo.getBar());
  21.  
  22. try {
  23. Field field = foo.getClass().getDeclaredField("bar");
  24. field.setAccessible(true);
  25. field.set(foo, "baz");
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29.  
  30. System.out.println(foo.getBar());
  31. }
  32.  
  33. }
Success #stdin #stdout 0.08s 40976KB
stdin
Standard input is empty
stdout
foobar
baz