A good development practice can be to use factory methods instead of class constructors. Factory methods can be more descriptive than constructors and can perform additional functionality over constructors such as returning immutable objects or objects that implement a certain interface.
First we will need a Java project so that we can replace a class constructor with a factory method. We will use the same project from the earlier recipe, Rename refactoring, and so ensure that you have the project open from the end of that recipe. If you have not followed that recipe, the project is available from the code download bundle under the MoveRefactor
folder in Chapter 3
. When the Projects explorer shows the Refactoring
project, expand the Refactoring
node if not yet expanded.