mjs
mjs

Reputation: 22347

Eclipse, refactoring a java method into a another class

How can I refactor(move) a Java method in classA into classB and have all references to the method to it updated?

Is this supported in Eclipse ?

Upvotes: 35

Views: 30337

Answers (2)

Thomas
Thomas

Reputation: 5094

For a static method, you can right click and select 'Move'.

Obj1.myMethod()

would then get 'moved' to

Obj2.myMethod()

and eclipse would fix your imports etc.

For a non-static method, this may not work depending on the relationship between classA and classB.

Obj1 myobj1 = new Obj1();
myobj1.myMethod();
myobj1.myOtherMethod();

If you move myMethod() to a different class, the refactoring would have to change the object initialization. If myOtherMethod isn't getting moved, then it can't just change the type of myobj1 to Obj2 because then myOtherMethod won't work.

Upvotes: 25

Deepak Azad
Deepak Azad

Reputation: 7923

  • Select the method in Outline view
  • Refactor > Move

If you want to move the method to a new class - Refactor > Extract Class

Upvotes: 11

Related Questions