aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why classes are declared final. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why classes are declared final." Watch "Why classes are declared final." New topic
Author

Why classes are declared final.

Ruchika Kapoor
Greenhorn

Joined: Oct 03, 2004
Posts: 22
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
Shubhada Nandarshi
Ranch Hand

Joined: Jun 10, 2005
Posts: 59
final classes can't be overwritten. so, if we call such classes they give fast performance than other classes which are not final.


Shubhada
Matt Russell
Ranch Hand

Joined: Aug 15, 2006
Posts: 165
IBM DeveloperWorks article: Guidelines for the effective use of the final keyword


Matt
Inquisition: open-source mock exam simulator for SCJP and SCWCD
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Final classes are immutable. I agree.


Can you please explain why final classes are immutable?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ruchika Kapoor
Greenhorn

Joined: Oct 03, 2004
Posts: 22
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

Joined: Oct 03, 2004
Posts: 22
Is anybody there to help me!
Praveen Babu
Ranch Hand

Joined: Jul 30, 2006
Posts: 138
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 ]
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Oct 03, 2004
Posts: 22
Thanks to all of you guys for being so patient in clarifying my doubt especially to Barry.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why classes are declared final.