7

This must be possible but I just can't figure it out in Eclipse. I have

/** ClassA.java */ class A { ... } class B { ... } 

and I'd like to select class B and extract to its own file so that I get:

/** ClassA.java */ class A { ... } 

and

/** ClassB.java */ class B { ... } 

How do I do this in Eclipse?

5 Answers 5

12

This is how you do it (works in Eclipse 3.5):

  1. Select your code to extract:

    /** ClassB.java */ class B { ... } 
  2. Cut

  3. Right click on the package in which you want to put it

  4. Select paste. (Then Organize imports if needed.)

Sign up to request clarification or add additional context in comments.

3 Comments

I like that, it avoids having to create the file. Odd that Eclipse is so inconsistent as to when the imports are properly copied over. If you manually paste into the file, any imports will follow. Here, they aren't copied over.
I suggested the cut-paste option but I didn't know of step 3 and 4. Thanks for that....
Strange that it doesn't have a quick-command for it like Alt-Shift-M for methods. But hey, learn something new every day. Thanks!
6

In my Eclipse (3.6 - Helios SR1), I highlight the type name (B), and then right-click for the pop-up menu and then select Refactor > Move Type to New File ....

Edit: It was called 'Convert Member Type to Top Level' in earlier verions, but has been updated to work with more than just member types:

http://download.eclipse.org/eclipse/downloads/drops/R-3.6-201006080911/eclipse-news-part2.html

Move type to new file refactoring.

The Convert Member Type to Top Level refactoring has been renamed to Move Type to New File and now allows any secondary type in a file to be moved into its own file. The action continues to work for member types.

5 Comments

Hmm, I wonder what plugin is providing that. I don't have that option in my stock install.
That's funny - I don't have that option. What version of Eclipse do you have?
It's called that in 3.6, it had a different name before.
Follow-up: Seems to be a new feature in Eclipse 3.6: download.eclipse.org/eclipse/downloads/drops/R-3.6-201006080911/…
@Ztyx, ah yes, and it's not just renamed from "Move member type to top level..." because in your case it's not a member type you're trying to move. It really is new functionality.
1

If there's no specific function, then creating the new class, copy and pasting the contents of B and then hitting CTRL-SHIFT-O on both files (or at least A.java) to clean up the imports should do it.

It's not a very common thing to ask for, and it's pretty easy to do manually.

Edit: You can also create B.java and then use Refactoring->Move on B in your A.java to move it to B.java. It didn't seem to copy over the imports though when I did that, whereas when I copied and pasted manually it grabbed the imports automatically.

1 Comment

I tried Moving B class out of A but that never worked, so I left it out of my solution.
1

There's no functionality that I know of. You will have to create a java file of choice and cut-paste the class in the new file. Fix the imports and package declaration names (if need be).


EDIT The best way to do it: Simply create a blank file B.java, go back to A.java, highlight B.java and right click and select Refactor -> Move (Alt+Shift+V for short). It will move the class B to B.java.

I have tested and it works. You might need to manage imports if necessary.

2 Comments

That's funny. Even though it's quite simple to do, you would think there was a shortcut for it. Seems like a common operation.
It is a common operation. Hence IDEA supports it since ages.
0

Position the caret somewhere inside class B (maybe it has to be inside the class name). Select Refactor -> Move type to new File.

(That's the action name in Eclipse 3.6. I believe in earlier versions it was Move member type to top level)

Reference:

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.