IntelliJ IDEA 14.1.0 Help

Extract Method Object

The Extract Method Object refactoring moves method into a new class, converting all the local variables to its fields, allowing you to decompose the method into other methods on the same object. It is an alternative to the Extract Method, and can be used when you have multiple return values in an extracted method.

Example

Before After
class Account { int val1, ...) { mputations return c-2*a;
class Account { int val1, ...) { tions calculations = new Calculations(val1, ...).invoke(); calculations.getC(); calculations.getA(); c-2*a; ss Calculations { int val1; int a; int c; Calculations(int val1, ...) { s.val1 = val1; int getA() {return a;} int getC() {return c;} Calculations invoke() { .//computations return this;

To extract a method object

  1. In the editor, select the method code block to be extracted into the object.
  2. On the main menu, or from the context menu of the selection, choose Refactor | Extract | Method Object.
  3. Select whether you want to create inner class, or anonymous class.
  4. If you want to create an inner class, you need to specify the name for the class and the visibility scope. You can also make the class static, if needed.
  5. If you want to create an anonymous class, you should specify method's name.
  6. In the Parameters area select the variables that will be used as a parameters.
  7. Review Signature Preview and click OK.

See Also

Last modified: 24 March 2015
comments powered by Disqus