IntelliJ IDEA 2024.2 Help

Extract parameter

The Extract Parameter refactoring lets you extract a new parameter to a method.

You can also use the Extract Functional Parameter refactoring. In this case, IntelliJ IDEA analyzes the selected code, finds out what the method signature would be for the extracted fragment, finds all functional interfaces with this method signature and wraps the code fragment with an anonymous class based on the selected interface and uses this anonymous class as a parameter.

  1. In the editor, place the caret within the expression that you want to introduce as a parameter and press Ctrl+Alt+P or go to Refactor | Extract | Parameter in the main menu.

    Alternatively, select a code fragment. On the toolbar that appears, click Extract and select Parameter.

  2. If you want to extract a functional parameter, press Ctrl+Alt+Shift+P or go to Refactor | Extract | Functional Parameter in the main menu.

  3. Specify a name of the parameter, select the necessary options in the Extract Parameter popup. When you invoke this refactoring, IntelliJ IDEA also displays the Gear icon that you can use to configure more options.

    Introduce Parameter: more options

    For example, if you don't want to change the existing method calls, select the Delegate via overloading method checkbox.

  4. If you need, press Shift+Tab to change a type of the new parameter. (If, at this step, you want to return to editing the parameter name, press Tab.)

Extract parameter examples

When extracting a new parameter you can either introduce a parameter into the existing method or use the Delegate via overloading method option to save the original method and define a new method with your parameter.

Let's replace the string value "Hello, World!" in the method generateText () with the new parameter text. The value "Hello, World!" is passed to the method in the updated method call generateText("Hello, World!").

Before

After

public class HelloWorldPrinter { public static void print() { System.out.println(generateText()); } private static String generateText() { return "Hello, World!".toUpperCase(); } }
public class HelloWorldPrinter { public static void print() { System.out.println(generateText("Hello, World!")); } private static String generateText(String text) { return text.toUpperCase(); } }

Before

After

object HelloWorldPrinter { fun print() { println(generateText()) } private fun generateText(): String { return "Hello, World!".toUpperCase() } }
object HelloWorldPrinter { fun print() { println(generateText("Hello, World!")) } private fun generateText(text: String): String { return text.toUpperCase() } }

Now let's use Delegate via overloading method. In this case, a new overloading method is created and the new parameter is extracted in the definition of this method (the second of the generateText() methods). The signature of the existing generateText() method is not changed. However, the method itself has been modified. Now, it calls the new generateText() method and passes the value "Hello, World!" to it in this call. Note that the existing call of generateText() (in the method print()) is not changed.

Before

After

public class HelloWorldPrinter { public static void print() { System.out.println(generateText()); } private static String generateText() { return "Hello, World!".toUpperCase(); } }
public class HelloWorldPrinter { public static void print() { System.out.println(generateText()); } private static String generateText() { return generateText("Hello, World!"); } private static String generateText(String text) { return text.toUpperCase(); } }

Before

After

object HelloWorldPrinter { fun print() { println(generateText()) } private fun generateText(): String { return "Hello, World!".toUpperCase() } }
// if you selected the "Introduce default value" option object HelloWorldPrinter { fun print() { println(generateText()) } private fun generateText(text: String = "Hello, World!"): String { return text.toUpperCase() } }

Functional parameter examples

Let's perform the refactoring on System.out.println(s); within Hello.printHello().

IntelliJ IDEA finds all functional interfaces with the appropriate method signature (String) -> void and suggests that you select one of them. (In this example, the interface Person is selected.)

As a result, the selected fragment System.out.println(s); is wrapped with an anonymous class based on the interface Person. This anonymous class is passed as a parameter to the call to printHello() within printText().

Person is added as a parameter to printHello() and the initially selected code fragment is replaced with the call to the method of the interface sayHello().

Before

After

@FunctionalInterface public interface Person { public void sayHello (String s); } public class Hello { private void printHello () { String s="Hello"; System.out.println(s); } private void printText () { printHello(); } }
@FunctionalInterface public interface Person { public void sayHello (String s); } public class Hello { private void printHello(Person person) { String s = "Hello"; person.sayHello(s); } private void printText () { printHello(new Person() { public void sayHello(String s) { System.out.println(s); } }); } }

Special notes for the Extract Parameter refactoring

  • If you want a field to be provided as a new parameter in the method declaration, in a method call this field will be presented as a field of a class instance.

  • If a class member is inaccessible (for instance, in the example above the field is private), it will be inserted in a method call but will be highlighted as an error making this file uncompilable.

  • If you use for the Extract Parameter refactoring a field with a getter, you will be prompted with an extended dialog. The dialog has an option group Replace fields used in expressions with their getters:

    • Do not replace: None of the fields will be replaced with calls to the getter.

    • Replace fields inaccessible in usage context: Only the fields that cannot be directly accessed from the usage context will be replaced with calls to the getter.

    • Replace all fields: All fields will be replaced with calls to the getter.

  • Applying the refactoring on a local variable will call the Extract Parameter dialog with additional checkboxes:

    • Replace all occurrences (<number_of_occurrences> occurrences): If enabled, all occurrences of the selected variable will be replaced with a parameter and the Delete variable definition checkbox is enabled. Otherwise, only the selected variable usage will be replaced with a parameter.

    • Delete variable definition: If enabled, the variable definition will be deleted.

    • Use variable initializer to initialize parameter: If enabled, the variable initializer will be used to initialize the parameter in the method call.

Side effects

Using the Extract Parameter refactoring can have unexpected side effects if applied on class instances or expressions which are actual method parameters. For instance, in case of such code:

class AClass { int field; int method() { return field; } } class Usage { void method(List list) { int sum = 0; for(Iterator it = list.iterator(); it.hasNext(); ) { sum += ((AClass) it.next()).method(); } } }

The code after refactoring applied to the field field:

class AClass { int field; int method(int newfield) { return newfield; } } class Usage { void method(List list) { int sum = 0; for(Iterator it = list.iterator; it.hasNext(); ) { sum += ((AClass) it.next()).method(((AClass) it.next()).field); } } }

The iterator value is increased twice which is, actually, not the behavior you would expect.

However, IntelliJ IDEA can use a temporary variable successfully and resolve such cases as increment/decrement/assignment operations and the new keyword usage. For instance:

public int myMethod(List list) { return list.size(); } public void anotherMethod() { myMethod(new ArrayList()); }

Same code after refactoring looks as follows:

public int myMethod(List list, int newPar) { return list.size(); } public void anotherMethod() { final ArrayList list = new ArrayList(); myMethod(list, list.size()); }

The new variable list was created and all parameters used for the method call are provided using this variable.

Extract parameter dialog (Java)

Use this dialog to specify the settings for extracting a parameter.

Item

Description

Parameter of type

Specify the type of the new parameter. Usually, you don't need to change the type suggested by IntelliJ IDEA.

Name

Specify the name for the new parameter.

Replace all occurrences

Select this option to replace all the occurrences of the selected expression within the method.

Declare final

Select this option to declare the parameter final.

Delegate via overloading method

Select this option to keep the existing method calls unchanged.

As a result, a new overloading method will be created and the new parameter will be added to the definition of this method. The method with the old signature will be kept. The call to the new method will be added to the old method. The necessary value (or expression) will be passed to the new method in this call.

The following options are available if the expression contains a direct call to a class field that has a getter.

Do not replace

Select this option to use a direct call to the field regardless of the scope context.

Replace fields inaccessible in usage context

Select this option to use a call to the getter only where a field is directly inaccessible.

Replace all fields

Select this option to use a call to the getter.

The following options are available when the refactored expression contains local variables.

Delete variable definition

Select this option to remove the definition of a local variable.

Use variable initializer to initialize parameter

Select this option to use the default value of the selected variable in the corrected method calls.

Extract parameter dialog (Groovy)

Use this dialog to specify the options and settings related to the Extract Parameter refactoring in Groovy.

Item

Description

Type

Specify the type of the new parameter. Usually, you don't need to change the type suggested by IntelliJ IDEA.

Name

Specify the name for the new parameter.

Declare final

Select this option to declare the parameter final.

Delegate via overloading method

Select this option to keep the existing method calls unchanged.

As a result, a new overloading method will be created and the new parameter will be added to the definition of this method. The method with the old signature will be kept. The call to the new method will be added to the old method. The necessary value (or expression) will be passed to the new method in this call.

Remove parameter <name> no longer used

Select this checkbox to remove a parameter.

Use explicit return statement

This checkbox is active if the method returns a value. You can omit return keyword if it is the last return statement in the method. If you select this checkbox the keyword is returned.

Parameters

In this area, select parameters to be passed to the new method/function.

Move Up/Down

Use these buttons to change the order of the parameters.

Signature preview

In this read-only field, view the declaration of the new method/function.

Last modified: 26 May 2024