aspose file tools
The moose likes Java in General and the fly likes String Mutable with Reflection Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "String Mutable with Reflection " Watch "String Mutable with Reflection " New topic
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!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: String Mutable with Reflection
 
Similar Threads
Reflection Mechansim to access private data.
what actually?
about the javaString
Serialization
StringBuffer and String returned from a method doubt