0

Possible Duplicate:
Why is String final in Java?

I'm just wondering why java.lang.String is made final? Is it to prevent from being inherited? Why?

Community
  • 1
  • 1
Rendicahya
  • 4,205
  • 7
  • 36
  • 56

3 Answers3

2

You should not be extending the string class. Just write your own methods in some other class that manipulate strings.

The reason is that the string class is a stable one which should not be tampered with as you may re-define some methods which would have unknown side effects on some other transactions.

Bnjmn
  • 1,973
  • 1
  • 20
  • 34
2

Yes indeed. This allows code in security managers and classloaders to work with the String type without having to worry that it's actually dealing with a malicious subclass that's specifically designed to trick it into allowing evil code through.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
2

Aside from security aspects that were already mentioned, I suspect performance was another important reason. For older JVMs especially final classes (where all methods are final by definition) made it much easier to inline code on-the-fly. And since String is one of most heavily used objects, which affects overall performance of many applications, this was seen as an area where improvements would have big overall effect.

StaxMan
  • 113,358
  • 34
  • 211
  • 239