Article by Farhan Khwaja
One of the Good practices while programming using Java is listed below.
Consider Static Factory Methods instead of Constructors
The normal way for a client to obtain an instance of itself is to provide a public constructor. But apart from this their is another technique which should be the part of every programmer’s toolkit. A class can provide a static factory method, which is static method which returns the instance of the class.
Example :
The below method translates a boolean primitive value into Boolean object reference.public static Boolean valueOf(boolean b){ return b ? Boolean.TRUE : Boolean.FALSE;}
Note :
A class can have static method instead of, or in addition to, constructors.
Advantages of using static factory methods :1. Unlike Constructors, static-factory methods have names :
If the parameters to a constructor do not, in and of themselves, describe the object being returned, a static method with a well chosen name is easier to use and the resulting client code easier to read. For example, the constructor BigInteger(int, int, Random), which returns a BigInteger that is probably prime, would have been better expressed as a static factory method named BigInteger.probablePrime.
2. Static factory method do not require to create a new object each time they are invoked :
This allows immutable classes to use preconstructed instances, or to cahce instances as they are constructed as to avoid creating unnecessary duplicate objects. The Boolen.valueOf(boolean) method illustrates this technique; it never creates an object. This can greatly improve performance.
3. Static factory methods can return an object of any subtype of their return type :
This gives us the flexibility of choosing the class of the returned object. To understand the above advantage better lets take an example :
The class java.util.EnumSet introduced in 1.5 release, has no public constructor, but only static methods. They return one of the two implementations depending upon the size of the underlying enum type; if it has 64 or fewer elements, the static factories return RegularEnumSet instance, which is backed by single long; if enum type had 64 or more elements, the factories return JumboEnumSet instance, backed by a long array.
The existence of the two implementations are invisible to the client. So a future release could add third or fourth implementation with a different returned value.
4. Static factory methods reduce the verbosity of creating the parameterized type instances :
Unfortunately, we must specify the the type parameters when we invoke the constructor of a parameterized class even if they are obvious from context. This makes us provide the type parameters twice in quick succession :
Example :
Map<String, List<String>> m= new HashMap<String, List<String>>
This redundant specification quickly becomes painful as the length and complexity of the type parameter increase. But with static methods, the compiler can figure out the type parameters for us. This is called type inference. For example,
public static <K, V> HashMap<K, V> newInstance(){ return new HashMap<K, V>();}
The above statement can reduce the verbosity :
Map<String, List<String>> m=HashMap.newInstance();
Follow me on Code 2 Learn
Farhan Khwaja is a young Entrepreneur and professional blogger from Mumbai, India. He writes about programming languages, programming tutorials, tips and tricks and also writes about topics of Computer Science.
Code 2 Learn primarily focuses on Computer Sciecne, Programming Tips and tricks, Tutorials etc.
Ultimate Guide To Job Interview Answers
New: Higher Pricepoint & It Converts Even Better! You Make Over Per Sale With The 61% Commish. Successful Product On CB For 7 Years Now. Large “hungry Crowd” Of Buyers Always There. If You Have A Jobs Or Career Site, This Is A Winner.
Ultimate Guide To Job Interview Answers
Java In A Nutshell, 5th Edition
With more than 700,000 copies sold to date, Java in a Nutshell from O’Reilly is clearly the favorite resource amongst the legion of developers and programmers using Java technology. And now, with the release of the 5.0 version of Java, O’Reilly has given the book that defined the “in a Nutshell” category another impressive tune-up.In this latest revision, readers will find Java in a Nutshell, 5th Edition, does more than just cover the extensive changes implicit in 5.0, the newest version of Java
List Price: $ 44.95
Price: [wpramaprice asin="0596007736"]



