I've recently discovered an interesting way to create a new instance of an object in Google Guava and Project Lombok: Hide a constructor behind a static creator method. This means that instead of doing new HashBiMap()
, you do HashBiMap.create()
.
My question is why? What advantage do you have of hiding the constructor? To me I see absolutely no advantage of doing this, and it seems to break basic object creation principles. Since the beggining you create an object with new Object()
, not some Object.createMe()
method. This almost seems like creating a method for the sake of creating a method.
What do you gain from doing this?
A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed only once. It is called automatically before the first instance is created or any static members are referenced.
One of the important property of java constructor is that it can not be static. We know static keyword belongs to a class rather than the object of a class. A constructor is called when an object of a class is created, so no use of the static constructor.
A private constructor in Java is used in restricting object creation. It is a special instance constructor used in static member-only classes. 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.
One of the advantages of the static factory methods with private constructor(object creation must have been restricted for external classes to ensure instances are not created externally) is that you can create instance-controlled classes.
There are a number of reasons why you might prefer a static factory method instead of a public constructor. You can read Item 1 in Effective Java, Second Edition for a longer discussion.
EnumSet.of(E)
will return a different type if the emum type has very few elements vs if the enum type has many elements (Edit: in this particular case, improving performance for the common case where the enum doesn't have many elements)Integer.valueOf(x)
will, by default, return the same object instance if called multiple times with the same value x
, if x
is between -128 and 127.java.util.concurrent.Executors
.Collections
hides many types. Instead of having a Collections
class with many static methods, they could have created many public classes, but that would have been harder for someone new to the language to understand or remember. List<String> strings = new ArrayList<String>()
in Guava you can do List<String> strings = Lists.newArrayList()
(the newArrayList
method is a generic method, and the type of the generic type is inferred).For HashBiMap
, the last reason is the most likely.
This is usually done because the class actually instantiated by the create()
method might be different than the type upon which you are invoking the method. i.e. a factory pattern where the create()
method returns a specific subclass that is appropriate given the current context. (For example, returning one instance when the currrent environment is Windows, and another when it is Linux).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With