This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes string class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "string class" Watch "string class" New topic
Author

string class

Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Hi Folks,

What's the technical reason of keeping the string class as final..??
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3599
    
  14

So people can't extend it. Is there anything you'd like to add to the String class?
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Stephan van Hulst wrote:So people can't extend it. Is there anything you'd like to add to the String class?



Hi ,

I want to know, the technical reason...does it related to string pool..utilization also..?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3599
    
  14

The reason is that there's no point in extending String. String conceptually doesn't have any subtypes.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40


There are a few reasons -- but if I had to guess, I would say that security is probably a big one.

Imagine an applet trying to open a network connection. Applets, by default, can only go back to the originating source. So, when an applet tries to open a connection, the host is passed to the security manager for approval prior to opening the connection. If the String class weren't final, you can override the class to behave a certain way during the security check, and behave in another later.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

Saral Saxena wrote:I want to know, the technical reason...does it related to string pool..utilization also..?

Saral, I really think you should read the tutorials (or a good book) rather than just machine-gunning this site for ready-made answers to whatever question you have this hour.

ShowSomeEffort (←click).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Another important reason is because class String is immutable. If it were not final, then somebody could make a subclass of String that has mutable state, and it wouldn't be immutable anymore.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: string class
 
Similar Threads
How to remove spaces from string
More then the specified answer?
String Q
Split Method in String Class
How String Can Be A Reference Variable...