# Replace constructor with factory method

> **TL;DR**
> `Alt+Enter` (Windows), `⌥ ⏎` (macOS), `⌥ ⏎` (IntelliJ IDEA Classic (macOS)), `⌥ ⏎` (macOS System Shortcuts), `Alt+Enter` (XWin), `Alt+Enter` (GNOME), `Alt+Enter` (KDE), `Alt+Enter` (Emacs), `Alt+Enter` (Sublime Text), `⌥ ⏎` (Sublime Text (macOS)), `Alt+Enter` (NetBeans), `Alt+Enter` (Visual Studio), `⌥ ⏎` (Visual Studio (macOS)), `Ctrl+1` (Eclipse), `⌘ 1` (Eclipse (macOS)) | Replace constructor with factory method
>
>
>
> This refactoring is available only as an [intention action](intention-actions.html).

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.

Procedure:

1. Place the caret at the class constructor in the editor and press `Alt+Enter` (Windows), `⌥ ⏎` (macOS), `⌥ ⏎` (IntelliJ IDEA Classic (macOS)), `⌥ ⏎` (macOS System Shortcuts), `Alt+Enter` (XWin), `Alt+Enter` (GNOME), `Alt+Enter` (KDE), `Alt+Enter` (Emacs), `Alt+Enter` (Sublime Text), `⌥ ⏎` (Sublime Text (macOS)), `Alt+Enter` (NetBeans), `Alt+Enter` (Visual Studio), `⌥ ⏎` (Visual Studio (macOS)), `Ctrl+1` (Eclipse), `⌘ 1` (Eclipse (macOS)).

2. From the list of available context actions, select Replace constructor with factory method.

![Replace constructor with factory method](https://resources.jetbrains.com.cn/help/img/idea/2026.2/replace_constructor_with_factory_method.png)

3. In the editor, specify the name of the factory method.

## Example

| Before | After |
| --- | --- |
|     ```JAVA // File Class.java public class Class {     public Class(String s) {         ...     } }  // File AnotherClass.java public class AnotherClass {     public void method() {         Class aClass = new Class("string");     } } ```    |     ```JAVA // File Class.java public class Class {     private Class(String s) {         ...     }     public static Class createClass(String s) {         return new Class(s);     } }  // File AnotherClass.java public class AnotherClass {     public void method() {         Class aClass = Class.createClass("string");     } } ```    |

## See also

### Refactoring

[Code refactoring](refactoring-source-code.html)

