aspose file tools*
The moose likes Beginning Java and the fly likes Why String methods and class both are final? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why String methods and class both are final?" Watch "Why String methods and class both are final?" New topic
Author

Why String methods and class both are final?

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Dear All

As we know if we make class final the class cant be extend by another class

so my question is if we make class final what is the use of making methods final even though we cant override that method because we cant extending the class
so why in java API String methods are final


Creativity is nothing but Breaking Rules
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61652
    
  67

santhosh.R gowda wrote:... we cant override that method bcz we cant extending the class ...

Please use real words when posting to the forums. Abbreviations such as "bcz" in place of "because" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please click this link ⇒ UseRealWords for more information.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
James Basller
Ranch Hand

Joined: Sep 07, 2008
Posts: 58
santhosh.R gowda wrote:Dear All

As we know if we make class final the class cant be extend by another class

so my question is if we make class final what is the use of making methods final even though we cant override that method because we cant extending the class
so why in java API String methods are final


Hi

Because Strings are immutable means when we create any string it will occupy new space in the memory.

Thanks
James
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Dear James

Please can you explain your answer more clearly...
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Check this journal article.


[My Blog]
All roads lead to JavaRanch
James Basller
Ranch Hand

Joined: Sep 07, 2008
Posts: 58
santhosh.R gowda wrote:Dear James

Please can you explain your answer more clearly...


When we create any string literal with respect to memory efficiency JVM creates its own String Constant Pool. Now, this pool contains the literals and if the same string found by the JVM it will check whether the string is already there in the pool if there then it will not create any new string and take reference of the same string which is already present in the pool.

So, to make sure that strings already there in the String Constant Pool remains unchanged with blocking overriding operations String class is marked as final.

Thanks
James
Virendrasinh Gohil
Ranch Hand

Joined: Jun 09, 2004
Posts: 46
santhosh.R gowda wrote:Dear All

As we know if we make class final the class cant be extend by another class

so my question is if we make class final what is the use of making methods final even though we cant override that method because we cant extending the class
so why in java API String methods are final


Sorry to ask this, but have you seen even a single method of String class final? I explored entire source code of String class and can't find even single method declared final. It's the class and inner variable (except hash) are declared final.

I think you might wanted to say, String class doesn't provide even a single method which gives mutability to String object. (Which means, no setter, no method which modifies internal content or no getter which gives you hold of internal content to modify it but all these doesn't mean the methods are declared final).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
The class itself is final; if you cannot extend the class you cannot override any methods, so there is no point in marking the methods final.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Even when sometime you want to design your own class to be immutable, you will have to make the class 'final' and in case you have any mutable fields in it, you must have to declare it private,make a getter method for the same that will return a new instance of that mutable object - just to make sure your Object is never changed at all.
Object once created is created.


OCPJP 6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Vinoth Kumar Kannan wrote: . . . make a getter method for the same that will return a new instance of that mutable object - just to make sure your Object is never changed at all. . . .
That practice is discussed in this thread, where you find out its correct name
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

http://www.javapractices.com - a site worth browsing into.. found it there by accident!
Hope someone gets benefited from it as well.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

String class is immutable and so it is declared to be 'final' - very fine.
Then, why are the mutable classes StringBuffer & StringBuilder 'final' ? What wrong are we going to do by extending those classes and over-riding the methods? - I mean, why was it designed like that?
(Just thought this might be a question related to the topic discussed, so just brought it up..instead of a new thread)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
The designers of the platform obviously wanted nobody to extend those classes.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why String methods and class both are final?