| Author |
String Mutable with Reflection
|
Raj chiru
Ranch Hand
Joined: Aug 12, 2008
Posts: 140
|
|
Hi...
Is it possible to mutable the String objects with Java Reflection?
|
 |
W. Joe Smith
Ranch Hand
Joined: Feb 10, 2009
Posts: 710
|
|
|
I'm pretty sure that, no matter what, a String object is immuteable.
|
SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Actually Raj is right. With reflection you can get access to the private char[] value, and modify its contents. You can even modify multiple "different" strings that way, if they share this same char[].
Once you start using reflection to access non-public members, many of the guarantees made by any class are immediately void. That includes the immutability of String.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
W. Joe Smith
Ranch Hand
Joined: Feb 10, 2009
Posts: 710
|
|
Rob Prime wrote:Actually Raj is right. With reflection you can get access to the private char[] value, and modify its contents. You can even modify multiple "different" strings that way, if they share this same char[].
Once you start using reflection to access non-public members, many of the guarantees made by any class are immediately void. That includes the immutability of String.
Wow....learn something new every day!
|
 |
 |
|
|
subject: String Mutable with Reflection
|
|
|