• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why String methods and class both are final?

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64717
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Basller
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear James

Please can you explain your answer more clearly...
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this journal article.
 
James Basller
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The designers of the platform obviously wanted nobody to extend those classes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic