Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why System class declared as final and with private constructor? [duplicate]

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.

like image 444
Bacteria Avatar asked May 15 '15 11:05

Bacteria


People also ask

Should final class have private constructor?

Using Private Constructors to Prevent Subclassing If we tried to create such as subclass, it would be unable to call the super constructor. However, it's important to note that we'd normally make a class final to prevent subclassing rather than using a private constructor.

What happens if a class is declared as final?

The main purpose of using a class being declared as final is to prevent the class from being subclassed. If a class is marked as final then no class can inherit any feature from the final class. You cannot extend a final class.

What happens if we declare constructor as private?

If a constructor is declared as private, then its objects are only accessible from within the declared class. You cannot access its objects from outside the constructor class.

Can you have both public & private constructors in the same class?

Save this answer. Show activity on this post. You can use both private and public constructor only in following way. But you can't use both for no argument constructor or same argument type.


1 Answers

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.

like image 172
Erhan Bagdemir Avatar answered Sep 30 '22 16:09

Erhan Bagdemir