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 Head First Android 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: 203

Hi Folks,

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

Joined: Sep 20, 2010
Posts: 3884
    
  18

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


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 203

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: 3884
    
  18

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: 19925
    
  43


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: 8661
    
  23

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: 14913
    
  26

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: string class
 
It's not a secret anymore!