Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Why classes are declared final.

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Final classes are immutable. I agree. But what could be the real reason behind making a class final.

Can anybody pls elaborate on this.

Thanks,
RK
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
final classes can't be overwritten. so, if we call such classes they give fast performance than other classes which are not final.
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IBM DeveloperWorks article: Guidelines for the effective use of the final keyword
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Final classes are immutable. I agree.



Can you please explain why final classes are immutable?
 
Ruchika Kapoor
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barry,

Let me explain my doubt.

We all know that String objects are immutable or we can say that String class can not be extended. Now since the String class is declared final..
it prevents anyone to extend that class. My main concern is to know when to declare Classes as final and why String class is declared final.

Acc. to many declaring classes as final increases performance but I read somewhere that

Most Java texts properly describe the usage and consequences of using the final keyword, but offer little in the way of guidance as to when, and how often, to use final. In my experience, final is vastly overused for classes and methods (generally because developers mistakenly believe it will enhance performance), and underused where it will do the most good -- in declaring class instance variables.

Please help me to clarify my doubt.

Regards,
RK
 
Ruchika Kapoor
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is anybody there to help me!
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ruchika,

The main idea of making a class final is to restrict the user of your class from changing it. So if you feel, your class has a particular behavior which should not be changed, then you declare it to be final.

String class is final because if multiple references refer to the same object, then it is easier to change its value and hence the idea of maintaining the unique string literals in string contant pool will become useless.

I dont think that performance is an issue. Since the only problem while restricting a class to be final is the extensibility. The user cannot add to the behavior of your class.

Regards,
Praveen
[ September 18, 2006: Message edited by: Praveen Babu ]
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

The Real programming and constraints are always directly related to their usage in real time applications. The Desing patterns and OO practices are proven methods for the good design to build a better application.

For the answer why "final", you can search in Design patterns and OO principles, ... Some times we require , not to extend the functionality.
and some times, close for modification and open for extension.


Just explore ...
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class StringBuffer is a final class that is not immutable. You cannot extend it so you cannot override any of its methods. If the instances of StringBuffer were immutable like class String then StringBuffer would be useless. So making a class final protects the class from behavoural changes not state changes.

Whether or not to make a class or a method final is discussed in the article quoted above.
[ September 19, 2006: Message edited by: Barry Gaunt ]
 
Ruchika Kapoor
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all of you guys for being so patient in clarifying my doubt especially to Barry.
 
reply
    Bookmark Topic Watch Topic
  • New Topic