Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 950801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:37:34+00:00 2026-05-15T23:37:34+00:00

I have a class with a private static final field that, unfortunately, I need

  • 0

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.lang.IllegalAccessException: Can not set static final boolean field

Is there any way to change the value?

Field hack = WarpTransform2D.class.getDeclaredField("USE_HACK");
hack.setAccessible(true);
hack.set(null, true);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:37:35+00:00Added an answer on May 15, 2026 at 11:37 pm

    Assuming no SecurityManager is preventing you from doing this, you can use setAccessible to get around private and resetting the modifier to get rid of final, and actually modify a private static final field.

    Here’s an example:

    import java.lang.reflect.*;
    
    public class EverythingIsTrue {
       static void setFinalStatic(Field field, Object newValue) throws Exception {
          field.setAccessible(true);
    
          Field modifiersField = Field.class.getDeclaredField("modifiers");
          modifiersField.setAccessible(true);
          modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    
          field.set(null, newValue);
       }
       public static void main(String args[]) throws Exception {      
          setFinalStatic(Boolean.class.getField("FALSE"), true);
    
          System.out.format("Everything is %s", false); // "Everything is true"
       }
    }
    

    Assuming no SecurityException is thrown, the above code prints "Everything is true".

    What’s actually done here is as follows:

    • The primitive boolean values true and false in main are autoboxed to reference type Boolean "constants" Boolean.TRUE and Boolean.FALSE
    • Reflection is used to change the public static final Boolean.FALSE to refer to the Boolean referred to by Boolean.TRUE
    • As a result, subsequently whenever a false is autoboxed to Boolean.FALSE, it refers to the same Boolean as the one refered to by Boolean.TRUE
    • Everything that was "false" now is "true"

    Related questions

    • Using reflection to change static final File.separatorChar for unit testing
    • How to limit setAccessible to only “legitimate” uses?
      • Has examples of messing with Integer‘s cache, mutating a String, etc

    Caveats

    Extreme care should be taken whenever you do something like this. It may not work because a SecurityManager may be present, but even if it doesn’t, depending on usage pattern, it may or may not work.

    JLS 17.5.3 Subsequent Modification of Final Fields

    In some cases, such as deserialization, the system will need to change the final fields of an object after construction. final fields can be changed via reflection and other implementation dependent means. The only pattern in which this has reasonable semantics is one in which an object is constructed and then the final fields of the object are updated. The object should not be made visible to other threads, nor should the final fields be read, until all updates to the final fields of the object are complete. Freezes of a final field occur both at the end of the constructor in which the final field is set, and immediately after each modification of a final field via reflection or other special mechanism.

    Even then, there are a number of complications. If a final field is initialized to a compile-time constant in the field declaration, changes to the final field may not be observed, since uses of that final field are replaced at compile time with the compile-time constant.

    Another problem is that the specification allows aggressive optimization of final fields. Within a thread, it is permissible to reorder reads of a final field with those modifications of a final field that do not take place in the constructor.

    See also

    • JLS 15.28 Constant Expression
      • It’s unlikely that this technique works with a primitive private static final boolean, because it’s inlineable as a compile-time constant and thus the "new" value may not be observable

    Appendix: On the bitwise manipulation

    Essentially,

    field.getModifiers() & ~Modifier.FINAL
    

    turns off the bit corresponding to Modifier.FINAL from field.getModifiers(). & is the bitwise-and, and ~ is the bitwise-complement.

    See also

    • Wikipedia/Bitwise operation

    Remember Constant Expressions

    Still not being able to solve this?, have fallen onto depression like I did for it? Does your code looks like this?

    public class A {
        private final String myVar = "Some Value";
    }
    

    Reading the comments on this answer, specially the one by @Pshemo, it reminded me that Constant Expressions are handled different so it will be impossible to modify it. Hence you will need to change your code to look like this:

    public class A {
        private final String myVar;
    
        private A() {
            myVar = "Some Value";
        }
    }
    

    if you are not the owner of the class… I feel you!

    For more details about why this behavior read this?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have: public class Components<T> extends TupleList<Class<T>, String> { private static final
I have a private static readonly field in my class: public class MyClass {
I recently ran across a class that had the following field declared: private final
I have this class: public static class CsvWriter { private static StreamWriter _writer =
I have a class (singleton) and it contains a static Dictionary private static Dictionary<string,
I have simplified my code to this internal class Program { private static void
I have a class class foo { public: foo(); foo( int ); private: static
In PHP, I have following Singleton Database Class: class Database { private static $instance;
I have this small class looking like this: private static int field1 = -
Let's say I have a class called AppConfig: public static class AppConfig { private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.