• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Why String methods and class both are final?

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear James

Please can you explain your answer more clearly...
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this journal article.
 
James Basller
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Marshal
Posts: 80288
433
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80288
433
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80288
433
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The designers of the platform obviously wanted nobody to extend those classes.
 
crispy bacon. crispy tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic