Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying class members into other classes - eclipse

A way to move methods to another class

When you need to move some class functionality into another class, it might be easily done by searching for corresponding methods via references of some common variable (Ctrl+Shift+G) and then using Eclipse's refactoring functionality "Move" which allows to move selected methods into some other class.

But it also might happen that you need to copy methods. For instance if you need pretty alike functionality in other class - changing a bit these copied methods is way more comfortable than writing from scratch.

  • Copy-pasting method bodies one by one is not the most convenient approach..
  • Creating a duplicate class and then using "method moving" that was mentioned above, feels slightly dumb too.

How would/do you act?

PS. Don't think and talk of code duplication issues please, definitely i am not going to have common functionality in two separate methods. Just take it as challenge of copying batch of methods from class A to class B. For whatever reason. And the screenshot is just an illustration of "moving" functionality.

like image 413
Erik Kaju Avatar asked Jan 25 '26 03:01

Erik Kaju


1 Answers

  • Select one or several methods in the Outline view and copy them (it copies the entire method).
  • Open other class where you want methods to get copied. Navigate to outline view and right-click on class name -> paste. Methods' bodies get successfully copied into another class.
like image 188
mki Avatar answered Jan 26 '26 17:01

mki