I want to make String
class mutable, simplest way to do this is make inner char array is public. How can i achive it?
Asked
Active
Viewed 819 times
-2

fxrbfg
- 1,756
- 1
- 11
- 17
-
1That is completely impossible. http://xyproblem.info – SLaks Jun 20 '17 at 21:32
-
3You can't make Java `String` mutable. Use `StringBuilder` if you want a mutable sequence of characters. – Elliott Frisch Jun 20 '17 at 21:32
-
1Yep, use [StringBuilder](https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html) as @ElliottFrisch suggests. – Hovercraft Full Of Eels Jun 20 '17 at 21:35
-
You can get source code and change it. However, since all libraries assume that String is immutable, you also need to change and test them. – Jun 20 '17 at 21:41
-
1It's a violation of the Java license to distribute any libraries that include `java.` or `javax.` in the package name, or to distribute a version of Java that isn't compliant with the specification. – Lew Bloch Jun 20 '17 at 21:46
-
1It's possible to alter private fields in a class (like `String`) using reflection, but it's also a bad idea (usually). Specifically for `String` mutating the contents of the string will break a lot of code. `StringBuilder` (as mentioned) is a much better idea. – markspace Jun 20 '17 at 22:16
-
I added another one answer to previous question about it. – fxrbfg Jun 20 '17 at 22:19
1 Answers
-2
I'm pretty new to this so sorry if this is wrong.
Wouldn't it be possible to inherit from the String class then override the required method?
Adam
-
2
-
1Always check the documentation first: https://docs.oracle.com/javase/8/docs/api/java/lang/String.html – Lew Bloch Jun 20 '17 at 21:47