IntelliJ IDEA 2022.2 Help

Make Static

The Make Static refactoring converts an inner class or an instance method to a static one.

For class, this refactoring also automatically corrects all references to the class in the code.

For method, this refactoring also automatically corrects all calls, implementations and overridings of the method.

  1. Select a method or a class that you want to refactor.

  2. On the main or context menu, select Refactor | Make Static.

  3. In the dialog that opens, specify the refactoring options.

    • For a class, if the class references any outer class fields, IntelliJ IDEA suggests to pass the outer class as a parameter to the inner class constructor.

    • For a method, if the method references any of the containing class fields, you can pass the whole referenced object as a parameter to the method (Add object as a parameter with name) or pass referenced fields/variables as parameters to the method (Add parameters for fields).

      If the method does not contain any references to fields or instance variables, you can specify whether or not you want to replace instance qualifiers with class references.

  4. Preview and apply your changes.

Make method static examples

Before

After

class ConnectionPool { public int i; public int j; public void getConnection() { ... } }
class ConnectionPool { public int i; public int j; public static void getConnection(ConnectionPool connectionPool) { ... } }
class ConnectionPool { public int i; public int j; public void getConnection() { ... } }
class ConnectionPool { public int i; public int j; public static void getConnection(int i, int j) { ... } }

In call hierarchies, if the method callers don't contain any other references to instance members, IntelliJ IDEA suggests that you make those callers static too. In this example, the refactoring is performed on baz(int i). All the caller methods are selected for making static too. The appropriate dialog lets you select the caller methods to be made static.

Before

After

class CallHierarchySample { private void foo(int i) { bar(i);} private void bar(int i) { baz(i);} private void baz(int i) { } }
class CallHierarchySample { private static void foo(int i) { bar(i);} private static void bar(int i) { baz(i);} private static void baz(int i) { } }
Last modified: 29 November 2022