Java Final class or private constructor

1.1k Views Asked by At

When we want to close a class to inheritance we are declaring class with final,

final class Myclass {}

but when we declare the constructor private it will be the same effect,

class Myclass {
     private Myclass() {}
}

But is it really the same? Is there any difference for optimization of code or readability of code? And which classes has to close to inheritance,

  1. immutable class maybe
  2. every method and member variable declared static of class

But for second option java.util.Arrays hasn't been declared with final even if all methods of Arrays are declared static.

3

There are 3 best solutions below

2
On BEST ANSWER

but when we declare ctor private it will be the same effect

Not necessarily, consider this example using static classes:

public class SOExample {
    private static class ClassWithPrivateConstructor {
        private ClassWithPrivateConstructor() {
            System.out.println("I have a private constructor");
        }
    }

    private static class ClassThatExtendsClassWithPrivateConstructor extends ClassWithPrivateConstructor {
    }

    public static void main(String[] args) {
        new ClassThatExtendsClassWithPrivateConstructor();
    }
}

This will print "I have a private constructor".

but is it really same? is there any difference for optimization of code or readablity of code. and which classes has to close to inheritance,

When a developer sees that a class is final, they understand that the intention of the author was that it should not be extended.

When a developer sees that a class has a private constructor, they understand that the class can't be instantiated. This is generally because it is either a static utility class or a singleton.

But for second option java.util.Arrays hasn't declared with final even if all methods of Arrays declared static

This is a good observation. My guess is that it probably should have been but can't be changed now for backward compatibility.

0
On

If you want to create an immutable class or just a class that for some reason should not have childrens you should declare it as final. Private constructor should be used in utility classes, this way you block inheritance and also make sure instance of the class cannot be created.

1
On

What I usually do is I make the class final and the constructor private:

  1. it removes the ambiguity about the class use (emphasising it's a utility class)
  2. it keeps the user away from what they aren't supposed to do (to initiate, to extend).

.

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class HttpTool {}

That said, there is nothing wrong with java.util.Arrays. The designer achieves the desired effect (non-instantiability) with the necessary minimum (the private constructor).