IntelliJ IDEA 2021.2 Help

Extract into class refactorings

IntelliJ IDEA lets you use refactorings that extract fields, methods, and parameters into a new class. These refactorings are useful when a class has grown too large and "does too many things". In such cases, it may be a good idea to split the class into smaller, more cohesive classes.

  1. Select a code fragment that you want to extract into a class.

  2. Depending on what you want to extract, from the main menu, select one of the following:

    • Refactor | Extract | Delegate

    • Refactor | Extract | Method Object

    • Refactor | Extract | Parameter Object

  3. In the dialog that opens, specify the name of a new class, method and parameter options.

  4. Preview your changes and click OK.

Extract delegate example

The Extract Delegate refactoring lets you extract some of the fields and methods of a class into a separate, newly created class.

Before

After

public class Foo { private String b; public String getInfo() { return ("(" + b + ")"); } ... } public class Bar { Foo foo; String t2 = foo.getInfo(); ... }
public class Foo { private final Info info = new Info(); public String getInfo() { return info.getInfo(); } ... } public class Info { private String b; public Info() {} public String getInfo() { return ("(" + b + ")"); } } public class Bar { Foo foo; String t2 = foo.getInfo(); ... }

Extract method object example

The Extract Method Object refactoring moves method into a new class, converting all the local variables to its fields, allowing you to decompose the method into other methods on the same object. It is an alternative to the Extract method, and can be used when you have multiple return values in an extracted method.

Before

After

class Account { int gamma (int val1, ...) { //some computations return c-2*a; } }
class Account { int gamma (int val1, ...) { Calculations calculations = new Calculations(val1, ...).invoke(); int c = calculations.getC(); int a = calculations.getA(); return c-2*a; } private class Calculations { private int val1; ... private int a; private int c; public Calculations(int val1, ...) { this.val1 = val1; ... } public int getA() {return a;} public int getC() {return c;} public Calculations invoke() { ...//computations return this; } } }

Extract parameter object example

The Extract Parameter Object refactoring lets you select a set of parameters to a method, and either create a wrapper class for those parameters, or use an existing compatible wrapper class. All calls to the method selected will have their parameters appropriately wrapped, and all usages of the wrapped parameter will be replaced by the appropriate calls on the newly created parameter class.

Before

After

public class A { private void drawEdge(Graphics g, float edgeWidth, int x1, int x2, int y1, int y2) { final Graphics2D g2d = (Graphics2D) g; g2d.setStroke(new BasicStroke(edgeWidth)); g.drawLine(x1, y1, x2, y2); } }
public class A { private void drawEdge(Edge edge, Graphics g) { final Graphics2D g2d = (Graphics2D) g; g2d.setStroke(new BasicStroke(edge.getEdgeWidth())); g.drawLine(edge.getX1(), edge.getY1(), edge.getX2(), edge.getY2()); } } public class Edge { private final float edgeWidth; private final int x1; ... public Edge(float edgeWidth, int x1, int x2, int y1, int y2) { this.edgeWidth = edgeWidth; this.x1 = x1; ... } public float getEdgeWidth() { return edgeWidth; } public int getX1() { return x1; } ... }
Last modified: 02 August 2022