7

As per my understanding

Final class

A final class is simply a class that can't be extended.

A class with single no argument private constructor

A class with private constructors cannot be instantiated except form inside that same class. This make it useless to extend it from another class. But it does not mean it cannot be sub classed at all, among inner classes we can extend and call the private constructor.

So my understanding is, if we create a class with single no argument private constructor, their is no meaning to declare that class as final. Then why System class in Java, declared as final class although it has single no argument private constructor?

I heard that making a class final has some performance gain. Is this correct and is this the only reason to declare the System class as final? Please clarify me why Java implemented the System class like this.

Bacteria
  • 8,406
  • 10
  • 50
  • 67
  • 3
    It is final so it cannot be extended, and it has a private constructor so it cannot be instantiated (because there should only be one `System`) - they're not really related. – Evan Knowles May 15 '15 at 11:27
  • private constructor use for creating singleton class and if you extend it then you can create object of superclass so for restricting that System object – nilesh virkar May 15 '15 at 11:31
  • 2
    I guess, it's just to make things clear for everybody: don't even try to extend it. I think it's unlikely that there are any performance gains in this particular case as `System` has no virtual methods, only static ones. – Tagir Valeev May 15 '15 at 11:36
  • Did you search first? [This](http://stackoverflow.com/questions/2956425/private-constructor-and-final) and [this](http://stackoverflow.com/questions/18523248/java-private-constructor-vs-final-and-more) on SO; [this](http://www.coderanch.com/t/516368/java/java/Difference-private-constructor-making-class) and [this](http://www.coderanch.com/t/384755/java/java/Final-Class-Class-private-constructor) outside. – user1803551 May 15 '15 at 11:48

1 Answers1

5

Sometimes, you need some "utility" classes, which contain some static utility methods. Those classes are not expected to be extended. Thus, developers may make some defensive decisions and mark such classes "final". I would say, marking a Java class with "final" may cause a tiny little performance improvements in Hotspot (please refer to https://wikis.oracle.com/display/HotSpotInternals/VirtualCalls). But, I am pretty sure, that it was a design decision rather than performance.

You can use final classes, if you want to have some immutable value objects as well, or if you want to make the classes be instantiated only through their factory methods. Immutable value objects are especially very useful while dealing with concurrency:

public final class MyValueObject {
   private final int value;
   private MyValueObject(int value) { this.value = value; }
   public static MyValueObject of(int value) { return new MyValueObject(value); }  
}

Using the static factory method, you can hide all those construction details, behind the factory method. Moreover, you can control, through that factory, the number of instances of that class in runtime - as in singletons.

As I told, all these are design decisions. I am pretty sure that there is no remarkable performance gain brought by final classes.

Erhan Bagdemir
  • 5,231
  • 6
  • 34
  • 40