Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can eclipse extract a second class in class file to its own file

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.

like image 403
Bill K Avatar asked Sep 18 '08 23:09

Bill K


People also ask

How do I get .class files from Eclipse?

If you look in the bin subdirectory, you'll see the class files. Usually eclipse does this for you. If you go to the eclipse build path (right click properties on your project -> build path) and then select 'source', it should show an output folder.

How do I move a class from one class to another in Eclipse?

To move this method to the new class, highlight the method's name and select Refactor→ Move, or right-click and select Refactor→ Move, opening the Move Static Member(s) dialog shown in Figure 4-5.

Can we run class file in Eclipse?

You can't use Eclipse to run an external class file with a main method without creatin a java project with this class in its class path. This would give Error: Could not find or load main class classname.

How can I see all classes in Eclipse?

Use Ctrl + Shift + H keys to search for classes.


1 Answers

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.

like image 51
scubabbl Avatar answered Sep 24 '22 19:09

scubabbl