I have worked with Eclipse IDE for a long time and I really like it. It helps me deal with a lot of problems in Java programming. In this tutorial, I will share with you all what I did to setting up Eclipse IDE for first time.
We need format our code follow a standard then other one can read and maintenance your code easily.
To setup formatter in Eclipse IDE, you should go to Preferences -> Java -> Code Style -> Formatter.
By default, Eclipse has its own formatter. You can use the default formatter configuration provided by Eclipse or you can build your formatter configuration.
I can share my formatter configuration here.
Setup Save Actions
This is one of great features of Eclipse. It helps us do something when we press Save button to save a file, for example: remove unnecessary import commands for unused imports, format the source code, remove unnecessary casts.
To setup it, go to Preferences -> Java -> Editor -> Save Actions.
By default, the option “Perform the selected actions on save” is un-checked. We need to check it.
Here, we have 3 child options as:
- Format source code
By default, this option is disable. You can enable it by checking.
Format all lines: that means when we save our source code, Eclipse will re-format all code in current file.
Format edited lines: only re-format the code which was added or modified.
Both will use the Eclipse formatter configuration as I was mentioned in previous section.
- Organize imports
In Eclipse, we can order the import commands by using Organize Imports feature.
This option allow us re-order our import commands using the Organize Imports configuration.
- Additional actions
By default, this option is disable too and it configures some actions like: remove unnecessary casts, add some missing annotations. You can enable it by checking.
You can edit or add more actions which are allowed by clicking Configure… button.
Below is my Save Actions configuration:
This setup is very helpful for us to detect some problems we may have when programing with Java.
To setup it, go to Preferences -> Java -> Compiler -> Errors/Warnings.
- Code style
- Potential programming problems
- Name shadowing and conflicts
- Deprecated and restricted API
- Unnecessary code
- Generic types
- Null analysis