Replace constructor with factory method
Last modified: 20 September 2022Alt+Enter | Replace constructor with factory method
This refactoring is available only as an intention action.
The Replace Constructor With Factory Method refactoring lets you hide a constructor and replace it with a static method which returns a new instance of a class.
Place the caret at the class constructor in the editor and press Alt+Enter.
From the list of available context actions, select Replace constructor with factory method.
In the dialog that opens, specify the name of the factory method and the class where the method should be created.
Preview and apply the changes.
Example
Before | After |
---|---|
|
|
Replace constructor with factory method dialog
This dialog appears when you invoke the Replace constructor with factory method refactoring.
Item | Description |
---|---|
Factory method name | Specify here the name for the factory method. |
In (fully qualified name) | Specify here the class, where the method will be created. |
Thanks for your feedback!