12

I was recently going through one of the Netflix open source project

There I found use of both final class along with private constructor. I fully aware that

  1. final is to avoid inheritance
  2. private is to disallow instantiation

But m just curious to know why they are both used together. Although methods are static, so we can use them without instantiation but still eager to know design principle behind it.

6
  • 2
    How about a singleton class which Netflix didn't want anyone to extend for some reason? Commented Aug 22, 2016 at 11:57
  • 7
    "private is to disallow instantiation" This is not correct. The class itself can use the constructor, for example in a factory method.
    – Tom
    Commented Aug 22, 2016 at 11:59
  • It's most likely a class with utility methods (which are all static), and such a class should not be extended or instantiated. This is a common pattern, although it's not very object oriented.
    – Jesper
    Commented Aug 22, 2016 at 11:59
  • Making the class final and the constructor private does actually disallow instantiation, if the class is not using it now
    – Tobi
    Commented Aug 22, 2016 at 12:01
  • Prior to Java 1.5, it was a common way to create typesafe enum classes, such as TextAttribute, FileChannel.MapMode, and HTML.Attribute.
    – VGR
    Commented Aug 22, 2016 at 13:37

2 Answers 2

9

With this code you will have this features

  • Not allow anyone subclass (extends) your class
  • Not allow instantiating your class
  • Making a variables or classes final increase the performance (not much, but it does and used as common practice in big projects will make a difference)

In this case I can't see a singleton pattern to get an instance, so, IMHO, you're looking to a helper/util class in the Netflix API, where the developer team used some standard practices to ensure users use their classes in the correct way:

StaticFinalClassExample.methodYouWantToCall();

Also, looking at the class you linked:

/**
 * This class consists exclusively of static methods that help verify the compliance of OP1A-conformant....
 */

And:

//to prevent instantiation
private IMFConstraints()
{}

ADD ON:

If you want further info, take a look at Item 4 from Joshua Bloch's Effective Java (2nd Edition):

Item 4: Enforce noninstantiability with a private constructor

Occasionally you’ll want to write a class that is just a grouping of static methods and static fields. Such classes have acquired a bad reputation because some people abuse them to avoid thinking in terms of objects, but they do have valid uses.

  • They can be used to group related methods on primitive values or arrays, in the manner of java.lang.Math or java.util.Arrays.
  • They can also be used to group static methods, including factory methods (Item 1), for objects that implement a particular interface, in the manner of java.util.Collections.
  • Lastly, they can be used to group methods on a final class, instead of extending the class.

Such utility classes were not designed to be instantiated: an instance would be nonsensical. In the absence of explicit constructors, however, the compiler provides a public, parameterless default constructor. To a user, this constructor is indistinguishable from any other. It is not uncommon to see unintentionally instantiable classes in published APIs.

Attempting to enforce noninstantiability by making a class abstract does not work. The class can be subclassed and the subclass instantiated. Furthermore, it misleads the user into thinking the class was designed for inheritance (Item 17).

There is, however, a simple idiom to ensure noninstantiability. A default constructor is generated only if a class contains no explicit constructors, so a class can be made noninstantiable by including a private constructor.

4
  • I would like to know whether it is some kind of pattern followed by industry or Netflix guys created for their convenience. If in future I were to use this pattern what would be the scenario or test case?
    – Sudip7
    Commented Aug 24, 2016 at 8:17
  • @Sudip7 please check my update for further info, this is not actually a named pattern but a Joshua Bloch recommendation in his Effective Java book. What (IMO) is more than a good point to follow it. Commented Sep 12, 2016 at 13:57
  • Well, that sounds more convincing now. Thanks for the extracts from the great book. I wonder any such book available for latest java language.
    – Sudip7
    Commented Sep 14, 2016 at 12:10
  • @Sudip7 i'm sure effective java will have a 3rd edition soon, (please Joshua, work!!! :)). Commented Sep 14, 2016 at 12:12
0

That class consists of static so called "utility" methods, and therefore you don't need an instance of it, and further, it's WRONG to try to get an instance of it. The class is final so that a client developer doesn't have the option of coming along and extending the class, because that would be against the intention of the original class.

There are basically 2 uses for private constructors: to tightly control instantiation in the case of a class that you want to restrict creation of (for example, if it requires a ton of resources). In this first case, you have to provide static factory methods that create an object for the client. ie:

public static IMFConstraints getInstance()

The other case is if it's never valid to make an instance. In that case, you provide static methods, which are called on the class itself. ie:

public static void checkIMFCompliance(List<PartitionPack> partitionPacks)

You would call the above method like so:

// your cool client code here...
IMFConstraints.checkIMFCompliance(myPartitionPacks);
// more of your awesome code...

The class you linked is the latter case.

Not the answer you're looking for? Browse other questions tagged or ask your own question.