File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes final for any class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "final for any class" Watch "final for any class" New topic
Author

final for any class

Ramesh Kumar Swarnkar
Ranch Hand

Joined: Sep 15, 2003
Posts: 84
As the purpose of using final keyword before any class name, mqy be:
purpose 1. to make that class not extendiable further, or
purpose 2. to make that class immutable, by removing setter method from it.

My question is:
If I don't want to use 'final' keyword before any class name; can I achieve the above purposepurpose 1 by declaring its constructor as private ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You can't make a class immutable by putting final before the class name. Yes, you can prevent inheritance from a class by making all its constructors private.
But a user will see there is no "final" before the class name, and think it is possible to inherit from that class, so it is a bad idea to miss "final" out in that instance.
Ramesh Kumar Swarnkar
Ranch Hand

Joined: Sep 15, 2003
Posts: 84
Thanks !!

~ramesh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final for any class