37

I often refactor code first by creating an inner class inside the class I'm working on--When I'm done, I move the entire thing into a new class file. This makes refactoring code into the new class extremely easy because A) I'm only dealing with a single file, and B) I don't create new files until I have a pretty good idea of the name/names (Sometimes it ends up as more than one class).

Is there any way Eclipse can help me with the final move? I should just be able to tell it what package I want the class in, it can figure out the filename from the class name and the directory from the package.

This seems like a trivial refactor and really obvious, but I can't figure out the keystrokes/gestures/whatever to make it happen. I've tried dragging, menus, context menus, and browsing through the keyboard shortcuts.

Anyone know this one?

[edit] These are already "Top Level" classes in this file, not inner classes, and "Move" doesn't seem to want to create a new class for me. This is the hard way that I usually do it--involves going out, creating an empty class, coming back and moving. I would like to do the whole thing in a single step.

Bill K
  • 62,186
  • 18
  • 105
  • 157

5 Answers5

45

I'm sorry I gave the wrong answer before. I rechecked, and it didn't do quite want you want. I did find a solution for you though, again, in 3.4.

Highlight the class, do a copy CTRL-C or cut CTRL-X, click on the package you want the class do go into, and do a paste, CTRL-V. Eclipse will auto generate the class for you.

Convert Member Type to Top Level doesn't quite work. Doing that will create a field of the outer class and generate a constructor that takes the outer class as a parameter.

scubabbl
  • 12,657
  • 7
  • 36
  • 36
  • So close. That's exactly what I want, but paste is grayed in the package when I do that. I wonder if my version of eclipse is too old. 3.3.1.1? – Bill K Sep 19 '08 at 00:03
  • I just tested it in 3.3 and it did work. Hmm... maybe you missed copying the class line? – scubabbl Sep 19 '08 at 00:27
  • 2
    Got it! Only works in Package Explorer, not Navigator. Thank you very much! – Bill K Sep 19 '08 at 00:32
  • 3
    Convert Member Type to Top Level works as intended if you're converting a static inner class. Otherwise, Eclipse doesn't know for certain that you don't need the outer instance. – jamesh Dec 18 '08 at 21:23
37

In Eclipse 3.6, you can do: Refactor -> Move type to new file

Andy Huang
  • 371
  • 3
  • 2
  • +1 This was exactly what I was looking for! I had searched "eclipse extract inner class into outer class", this was the first question that came up, and this was the exact answer I needed. Thanks! – vergenzt Jan 31 '13 at 18:16
1

For IntelliJ IDEA / Android Studio: Refactor -> Move -> Move inner class MyInnerClass to upper level

Aditya Naique
  • 1,120
  • 13
  • 25
1

Right-click the class name (in the source code) and choose Refactor -> Convert Member Type to Top Level. It doesn't let you choose the package, though.

Matt Solnit
  • 32,152
  • 8
  • 53
  • 57
0

Can be done in 2 refactorings :

  1. Convert Member type to top level

  2. Move

Community
  • 1
  • 1
David Pierre
  • 9,459
  • 4
  • 40
  • 32