aspose file tools*
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 Spring in Action this week in the Spring 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: 3647
    
  17

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: 3647
    
  17

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: 18896
    
  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: 8008
    
  22

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: 14270
    
  21

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 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string class