The try-finally construction in Java is often used to close the resources. Finally block will be always executed, so no matter what has happened in the ‘try’ block, the file or connection will always be closed. However, since Java7, there is an easier and more succinct way to do it – try-with-resources. Not only does […]
Read moreCategory Archives: Effective Java
Avoid finalizers and cleaners
There is a mechanism in Java which allows running a certain code on object destruction. Something in a way like a C++ destructor method. However, due to java memory design and implementation of garbage collection in different JVMs, you should never rely on it. You are not guaranteed when the “destructor” method will be called, […]
Read moreEliminate obsolete objects references
Java is a garbage-collected language. That means, that objects with no reference to them are cleared automatically. But it doesn’t make it a memory-leak free! Nulling the object to garbage-collect it should be an exception, not a norm. However, you still need to think about how the garbage collector works and how to design your […]
Read moreAvoid creating unnecessary objects
Just don’t do it. Don’t create new strings Don’t instantiate resource heavy classes in a loop Mind the unnecessary auto-boxing Make use of static fields or caching Make use of singleton pattern Opt for immutable objects and reuse them. This post is based on Joshua Bloch Effective Java Third Edition (get it on Amazon: https://www.amazon.com/Effective-Java-3rd-Joshua-Bloch/dp/0134685997)
Read morePrefer dependency injection to hard wiring resources
When creating new classes it is preferable to design them easy to test (easy to mock) and as independent from other elements as possible (decoupling). Even though it might seem at the beginning that the classes used inside will never change, it is still advice to code to interface, instead of the implementation; and to […]
Read moreEnforce noninstantiability with a private constructor
Sometimes we want to create a class that is non instantiable: a class of static helper methods or constants. Let’s see how to make sure that such a class is never instantiated. Why should’t we have separate helper method classes? In general, it is not considered a good object oriented design. But the world is not […]
Read moreEnforce the singleton property with a private constructor or an enum type
A singleton is an object that is instantiated once and once only during runtime of an application. How can it be achieved? There are three common ways to do it: public static final field instance, static factory, enum. What is the easiest way to implement a singleton? Have a public static final field instance and […]
Read moreConsider a builder when faced with many constructor parameters

Constructors with many parameters are hard to read. The only clue you have about what each parameter means is its order. It is not easily readable. Even more troubles emerge, when we have optional parameters. Then we start to drown in a sea of constructors with different set of parameters, a so called telescopic constructor […]
Read moreConsider static factory methods instead of constructors

Event though a constructor is a fairly standard and natural way of creating instances of a class, there are other ways to do it. And they might be sometimes better suited to do the job that needs to be done. You ill find this pattern in many libraries API, just think of Integer.valueOf() or getInstance(). All […]
Read more