Reputation: 22347
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
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
Reputation: 7923
If you want to move the method to a new class - Refactor > Extract Class
Upvotes: 11