IntelliJ IDEA 2018.1 Help

Convert to Instance Method

refactoring

Convert to Instance Method refactoring lets you convert a static method to a non-static, class instance method, with a class being the type parameter of the initial method.

  1. In the editor, place the caret on the declaration or usage of a method to be refactored. The method should be static and the types of its parameters should be the classes from the project. In other words, you cannot use such parameters types, as String.
  2. Do one of the following:
    • On the Refactor menu, choose Convert to Instance Method.
    • Right-click the method and select Refactor | Convert to Instance Method.
  3. The Convert to Instance Method dialog appears.
  4. In the Select an instance parameter list select the class you want the method to belong to after the conversion. All the usages of this class inside the method are replaced with this.
  5. To change the visibility scope of the converted method, select the new scope in the Visibility area. By default the converted method will have no scope declaration (equivalent to public ).
  6. Preview and apply changes.

Example

Consider classes MyClass, ClassB and ClassB residing in the same package. MyClass contains the following code.

As a result, MyClass and ClassB become converted.

Before After
convertToInstance1
convertToInstance2
convertToInstance3
Last modified: 25 April 2018

See Also