-2

I want to make String class mutable, simplest way to do this is make inner char array is public. How can i achive it?

fxrbfg
  • 1,756
  • 1
  • 11
  • 17
  • 1
    That is completely impossible. http://xyproblem.info – SLaks Jun 20 '17 at 21:32
  • 3
    You can't make Java `String` mutable. Use `StringBuilder` if you want a mutable sequence of characters. – Elliott Frisch Jun 20 '17 at 21:32
  • 1
    Yep, 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
  • 1
    It'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
  • 1
    It'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 Answers1

-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