As we all know that we can make static variable, method or class. We know what it can do. If we make a variable static then that variable scope is present for the entire run of the program. Its memory is shared by all the object of the class in which it is declared.
We make the method so that it can not be inherited. An inner class can be made static.
In this article, we will focus more on making the method static. What all advantages and disadvantages will we get.
Let’s get started.
For a client to obtain the instance of a class, it will provide the public constructor. But there is another way to do it is through a static factory method that returns the instance of the class. Making an instance with a static method have many advantages. Let’s discuss them.
The Advantage of a static factory method is as follows:
1. The static method has a name, unlike constructor.
We can use the static factory method to return the instance of the class instead of using the constructor. It not only gives a meaningful name to easily understand the client code. For example, we have a BigInteger class that has 6 constructors which are differentiated by the parameter list. But it has one constructor that has the same parameter list as the static method probablePrime that will return the probable prime. So If we haven’t made the static method then it will create the problem that we have two constructors with the same parameter with different functionality.
public static BigInteger probablePrime(int bitLength, Random rnd);
public BigInteger(int numBits, Random rnd);
The first statement gives the probable prime where a second statement gives the non-negative BigInteger. As both return the BigInteger, If made the constructor for that then it will create the confusion which constructor do what. We can rearrange the parameter list but this will be the bad option as we will always get confused always has to see the documentation. So the static method is boon in this case.
2. Each time we called the constructor it will create the new instance whereas static method can return the existing instance.
The static method can return the same instance on a repeated call. Classes that have this ability are instance controlled.
Instance control class helps in creating singleton class and immutable classes.
We make a singleton class so that we have the only one instance like we make database connection class as a singleton class so in the entire run of the program we have only one instance. A database connection is heavy operation cannot be made every time we use it. This can be achieved by a static method.
3. A static method can return the object of any subtype of its return type, unlike the constructor.
Advantage of this we don’t have to make our class public which help us to hide our implementation we will get very compact API.
4. Using a static factory method we can return the different object of subtype in every call.
By having a static method we can return the different object depending upon the condition. This gives us the advantage that if we add more subclasses it won’t affect the client. As he/she does not know about it.
The disadvantage of a static factory method is as follows:
- Classes with no public or protected constructor cannot be subclassed.
- In the documentation, it is hard to find which is the static method that can be used to instantiate the class, unlike constructor which stand out what it does.
Effective Java – by Joshua Bloch