IntelliJ IDEA 14.0.0 Web Help

Make Method Static

The Make Method Static refactoring allows you to convert an instance method to a static one and automatically corrects all calls, implementations and overridings of the method.

Example

Before After
class ConnectionPool { public int variable1; public int variable2; public void getConnection() { ... } }
class ConnectionPool { public int variable1; public int variable2; public static void getConnection (ConnectionPool connectionPool) { ... } }
Before After
class ConnectionPool { public int variable1; public int variable2; public void getConnection() { ... } }
class ConnectionPool { public int variable1; public int variable2; public static void getConnection (int variable1, int variable2) { ... } }

To make a method static

  1. Select the method to be refactored in the Structure view, or right-click the method name in the editor.
  2. On the main menu, or on the context menu of the selection, choose Refactor | Make Static . The Make Method Static dialog box opens.
  3. If the method references any of the containing class fields, you have following options:
    • To pass the whole referenced object as a parameter to the method, select the Add object as a parameter with name check box and enter the name for the parameter.
    • To pass the referenced fields/variables as parameters to the method, select the Add parameters for fields check box and select the appropriate fields in the list. You can also change the order of the parameters using the Move Up and Move Down buttons.
  4. If the method does not contain any references to fields or instance variables, you should only specify whether you want to replace instance qualifiers with class references or not using the corresponding check box.
  5. Preview results in the Find tool window and apply changes.

See Also

Reference:

Web Resources:

Last modified: 4 December 2014
comments powered by Disqus