Friday, 23 October 2015
Often we write the code and import the classes in the import section of the class file.
Code never stays unchanged for lifetime as change is the
only constant thing in programming.
Many times, after code refactoring or bug fixed, Eclipse IDE
will show a yellow underline for all unused imports at the top of your class
and said “The import xxx is never used“
When we change the code sometimes we forget to remove the
old imported classes under import section. Keeping something which is never
used in code is not considered a good practice.
Eclipse provides multiple options to get rid of those unused
imports. I will explain all the options available in eclipse to achieve this.
Option-1
This option is not much useful as it only works for single
line and not for all unused import in whole class.
- Open your class file
- Go to the line of unused import
- Press ctrl + 1 which is a eclipse shortcut of quick fix, this will display a drop down menu to fix this and you will see an option "remove unused imports"
- Click on "remove unused imports"
Option-2
This option will remove all unused imports from whole class.
- Open your class file
- Right click on code area
- Go to “Source”
- Click on “Organize Imports”
Option-3
This option will also remove all unused imports from whole
class.
- Open your class file
- Press Shift + Ctrl + O for PC or Command + Shift + O for Mac
Advance Options :
Option-4
This option will allow you to remove all unused imports when you save your class file
- Click on “Window”
- Click on “Preferences”
- Select & Expand “Java”
- Select & Expand “Editor”
- Click on “Save Actions”
- Check the option “Perform the selected actions on save”
- Check the option “Organize Imports”
- Click on “Apply” in Preference window
- Click on “OK” in Preference window
Above option also allows you to set preference for formatting source code and invoking other additional actions.
Option-5
This option will allow you to remove all unused imports from all class files of your package. It’s very nice and efficient option to achieve this in one go.
- Go to “Project Explorer”
- Right click on your package
- Go to “Source”
- Click on “Organize Imports”
Option-6
This option will allow you to remove all unused imports from all class files of your package/project. You can configure this setting in Eclipse [built-in] profile or you can create your own profile for the same. I will show you by creating your own profile and configuring it.
Configuration Actions:
- Click on “Window”
- Click on “Preferences”
- Select & Expand “Java”
- Select & Expand “Code Style”
- Click on “Clean Up”
- Click on “New”
- Enter the “Profile Name” of your choice
- Click on “OK”
- Go to Tab “Code Organizing”
- Check the option “Organize Imports”
- Click on “Apply” in profile window
- Click on “OK” in profile window
- Click on “Apply” in preference window
- Click on “OK” in preference window
Executing Actions:
- Go to “Project Explorer”
- Right click on your package
- Go to “Source”
- Click on “Clean Up”
- Choose a profile you configured.
- Click on “Next”
- Click on “Finish”
I hope these options will make your life easier to work with eclipse and make code neat and cleaner.
I will come up with more eclipse tips soon till then happy coding…
Cheers!!!
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment