Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

34
  • 53
    @thecoop, @HalfBrian: there is no doubt that this is EXTREMELY EVIL, but this example was chosen by design. My answer only shows how, in some circumstances, this is possible. The most disgusting example that I can think of is deliberate chosen with the hope that perhaps people would be instantly disgusted by instead of falling in love with the technique. Commented Jul 21, 2010 at 17:24
  • 76
    Yo, dawg. I heard you like reflection, so I reflected on field so you can reflect while you reflect. Commented Mar 22, 2011 at 20:35
  • 12
    Note that Boolean.FALSE is not private.Does this really work with "private final static" members? Commented Feb 15, 2013 at 13:11
  • 20
    @mgaert it does, but you have to use getDeclaredField() instead of getField() for target class Commented Aug 14, 2013 at 7:28
  • 15
    +1. For those who will try to change something like final String myConstant = "x"; and will fail: remember that compile time constants will be inlined by compiler so when you will write code like System.out.println(myConstant); it will be compiled as System.out.println("x"); because compiler knows value of constant at compile time. To get rid of this problem you need to initialize your constants at runtime like final String myConstant = new String("x");. Also in case of primitives like final int myField = 11 use final int myField = new Integer(11); or final Integer myField = 11; Commented Dec 2, 2013 at 18:15