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 673421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:35:38+00:00 2026-05-14T00:35:38+00:00

Specifically, I’m trying to create a unit test for a method which requires uses

  • 0

Specifically, I’m trying to create a unit test for a method which requires uses File.separatorChar to build paths on windows and unix. The code must run on both platforms, and yet I get errors with JUnit when I attempt to change this static final field.

Anyone have any idea what’s going on?

Field field = java.io.File.class.getDeclaredField( "separatorChar" );
field.setAccessible(true);
field.setChar(java.io.File.class,'/');

When I do this, I get

IllegalAccessException: Can not set static final char field java.io.File.separatorChar to java.lang.Character

Thoughts?

  • 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-14T00:35:39+00:00Added an answer on May 14, 2026 at 12:35 am

    From the documentation for Field.set:

    If the underlying field is final, the method throws an IllegalAccessException unless setAccessible(true) has succeeded for this field and this field is non-static.

    So at first it seems that you are out of luck, since File.separatorChar is static. Surprisingly, there is a way to get around this: simply make the static field no longer final through reflection.

    I adapted this solution from javaspecialist.eu:

    static void setFinalStatic(Field field, Object newValue) throws Exception {
        field.setAccessible(true);
    
        // remove final modifier from field
        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    
        field.set(null, newValue);
    }
    

    I’ve tested it and it works:

    setFinalStatic(File.class.getField("separatorChar"), '#');
    System.out.println(File.separatorChar); // prints "#"
    

    Do exercise extreme caution with this technique. Devastating consequences aside, the following actually works:

    setFinalStatic(Boolean.class.getField("FALSE"), true);
    System.out.format("Everything is %s", false); // "Everything is true"
    

    Important update: the above solution does not work in all cases. If the field is made accessible and read through Reflection before it gets reset, an IllegalAccessException is thrown. It fails because the Reflection API creates internal FieldAccessor objects which are cached and reused (see the java.lang.reflect.Field#acquireFieldAccessor(boolean) implementation).
    Example test code which fails:

    Field f = File.class.getField("separatorChar"); f.setAccessible(true); f.get(null);
    // call setFinalStatic as before: throws IllegalAccessException
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 346k
  • Answers 346k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer From the Sun Java Docs: So when should you use… May 14, 2026 at 6:06 am
  • Editorial Team
    Editorial Team added an answer You can use the PhoneStateLisenter to listen out for changes… May 14, 2026 at 6:06 am
  • Editorial Team
    Editorial Team added an answer To access the nested fields from params do the following:… May 14, 2026 at 6:06 am

Related Questions

Specifically, what commands do I run from the terminal?
Specifically, in VS 2008, I want to connect to a data source that you
Specifically, I have a model that has a field like this pub_date = models.DateField(date
Specifically, I want to copy a link (with text and location) and then to
Specifically, I'm looking for a client-side, JavaScript and / or Flash based multiple file

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.