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.
Select a method or a class that you want to refactor.
On the main or context menu, select
.In the dialog that opens, specify the refactoring options.
For a class, if the class references any outer class fields, IntelliJ IDEA suggests passing 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.
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) { }
} |
Make Class Static dialog
Use this dialog to specify options for the Make Static refactoring.
Item | Description |
---|---|
Replace instance qualifiers with class references | Specify whether you want to replace instance qualifiers with class references or not. |
Make Method Static dialog
Use this dialog to specify options for the Make Static refactoring.
Item | Description |
---|---|
Add object as a parameter with name | Select this checkbox, if you want to pass the whole referenced object as a parameter to the method, then specify the name for the parameter in the field below. |
Add parameters for fields | Select this checkbox to pass the referenced fields/variables as parameters to the method, then select the appropriate fields in the list. |
Move Up/Move Down | Use this buttons to reorder parameters in the list. |
Replace instance qualifiers with class references | Specify whether you want to replace instance qualifiers with class references or not. This checkbox is available if the method does not contain any references to fields or instance variables. |