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

  • Home
  • SEARCH
  • 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 9193427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:09:38+00:00 2026-06-17T21:09:38+00:00

The following code attempts to set a value to the private final char value[]

  • 0

The following code attempts to set a value to the private final char value[] field of the String class using Java 7.

package test;

import java.lang.reflect.Field;

public final class Test 
{
    static
    {
        try
        {
            Field value = String.class.getDeclaredField("value");
            value.setAccessible(true);
            value.set("Hello World", value.get("1234567890"));
        }
        catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e)
        {
            System.out.println(e.toString());
        }
    }

    public static void main(String[] args) 
    {
        System.out.println("Hello World");
    }
}

and it silently displays 1234567890 on the console and there is no question about it.


When I try to do the same thing using Java 6 like the following,

package test;

import java.lang.reflect.Field;

public final class Test
{
    static
    {
        try
        {
            Field value = String.class.getDeclaredField("value");
            value.setAccessible(true);
            value.set("Hello World", value.get("1234567890"));
        }
        catch (IllegalArgumentException e)
        {
            System.out.println(e.toString());
        }
        catch (IllegalAccessException e)
        {
            System.out.println(e.toString());
        }
        catch (NoSuchFieldException e)
        {
            System.out.println(e.toString());
        }
        catch (SecurityException e)
        {
            System.out.println(e.toString());
        }
    }
    public static void main(String[] args)
    {
        System.out.println("Hello World");
    }
}

it causes the following exception to be thrown.

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException

It works when the length of value.get("1234567890") in this statement value.set("Hello World", value.get("1234567890")); is grater than or equal to the String Hello World

For example,

If the following statement (as in the preceding code snippet)

value.set("Hello World", value.get("1234567890"));

is replaced by something like the following

value.set("Hello World", value.get("12345678901"));

So why doesn’t this work with Java 6 (or might be lower, I didn’t try) when the length of the second parameter of the set() method is less than the first one?

BTW, I can understand that dealing with private fields with reflection in this way is not recommended at all and is worst.

  • 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-06-17T21:09:39+00:00Added an answer on June 17, 2026 at 9:09 pm

    So why doesn’t this work with Java 6 (or might be lower, I didn’t try) when the length of the second parameter of the set() method is less than the first one?

    In Java 6, you’re managing to set the value character array to the new reference – but you’re not changing the other fields which specify the section of the char[] which the string refers to.

    I can’t remember the exact field names, but it’s something like this:

    char[] value;
    int offset;
    int count;
    

    So for a string which “covers” the entire character array, offset would be 0 and count would be value.length. Now if you replace value with a shorter char[], but don’t change count, then offset + count is beyond the end of the array… it’s outside the bounds of the array. This is done so that operations like substring don’t need to copy the character data – they just create a new object which refers to the existing array, with different offset / count values.

    In Java 7 from update 5 onwards, strings don’t have this offset/count concept; instead, each string has its own character array and the start and end are implicit. I suspect that’s why it works for you in Java 7.

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

Sidebar

Related Questions

I'm using the following code: $query = $dbh->prepare(INSERT into crm_order_errors .SET (order_id, number_of_attempts, last_attempt)
The following code snippet attempts to create a Tib DaemonManager connecting to a particular
Im using the following code to attempt to update several static cells in my
I am using the following code to attempt to get the font color of
I am using the following code to attempt to redirect a dynamic URL to
I'm using the following code in an attempt to show a dialog with a
The following code (without showing it all) is my first attempt at using classes.
I'm using the following code attached to a button, to attempt to open a
I have the following PowerShell code: function Get-SmoConnection { param ([string] $serverName = ,
The error I get is 'IllegalArgumentException occured' Can not set java.util.ArrayList field mi.types.ListOfObjects.objects to

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.