3

I found the below code snippet :

import java.lang.reflect.Field;
public class Test {
    public static void main(String[] args) {
        System.out.println("Inside main");
    }
    static {
        try {
            Field value = String.class.getDeclaredField("value");
            value.setAccessible(true);
            value.set("Inside main", value.get("Inside static bolck"));
        } catch (Exception e) {
            throw new AssertionError(e);
        }
    }
}

As per my understanding the output should be Inside static bolck but the output comes as Inside stat, the same character length of Inside main.
*If i increase the length of Inside main the output length is also getting increased.
Can anybody please explain? I do not have much knowledge in Reflection.

5
  • It prints Inside static bolck for me. What version of JDK/JRE are you using? Commented Mar 15, 2013 at 12:48
  • IBM 32-bit SDK for Windows, Java Technology Edition, Version 6 Commented Mar 15, 2013 at 12:50
  • 1
    I'll repeat NPE's question, as I am eager too -- In any event, where are you going with this? Commented Mar 15, 2013 at 12:52
  • I found the code snippet. I am not doing anything with this. This is just for my knowledge Commented Mar 15, 2013 at 12:53
  • Downvoater can you please put a comment so that i can improve my question.. Commented Sep 18, 2013 at 13:35

4 Answers 4

4

On my JDK, String also has a count member, which would need to be updated to reflect the new length.

There's also an offset field, which may or may not need updating (probably not in the case).

Finally, there a hash field, which will no longer be correct after you've changed value.

Since this code relies on the undocumented details of a particular implementation of String, it is very fragile and highly non-portable. For example (hat tip @assylias), Oracle have removed the count and offset fields in JDK 7u6. If you were to upgrade from 7u5 to 7u6, all of a sudden your code would behave differently.

Sign up to request clarification or add additional context in comments.

4 Comments

count disappeared in JDK 7u6 I think (for Oracle JDK). So on 7 update 6+ it will work but before it will behave as in the question.
I found the code snippet. I am not doing anything with this. This is just for my knowledge
It prints "Inside stat" for me, which ties in with what @NPE says above, which presumably means the OP is using a version with count in
Prints "Inside stat" for me too 1.6
1

The code makes assumptions about the actual implementation of the String class, e.g. that the class has a field called "value".

Since the internal state of the String class is not part of the API or the language specification, the actual implementation will vary between VMs from different vendors or even between different VM versions from the same vendor.

Comments

0

"Inside main" contains 11 characters in value[] (value[] is private field in String class)
String value[] was initialized in first place which you define String "Inside main". Now you are changing value[]'s value which is private instance variable in String by using reflection then it has to fit only 11 character from the string Inside static bolck

Comments

0

When you will vary "Inside main" you are allowed to value.get() the string <= "Inside main".Change -"Inside main" to "Inside main (8 spaces) " and it prints

"Inside static bolck"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.