When I made the switch from .NET to Java, I naturally had to switch IDEs. With moving to Java, I chose Eclipse. I was fortunate to have a great group of coworkers that took time out of their schedule to share shortcuts and tips to allow me to utilize Eclipse to the fullest extent.
One of the first shortcuts I found to be very beneficial was Ctrl+Shift+t, which enables you to type a class name and in return it will summarize related or matching files to open. In the sections below I have listed a few other tips for Eclipse that I found useful during the transition and in my experience with Eclipse.
Also, I highly recommend taking a few minutes to read Adi Rosenblum’s Developers Are Lazy (And How To Use That To Your Advantage With Eclipse) for some more great tips on Eclipse.
Code templates enable you to create multiple templates to generate code. Examples of common templates are:
- Getter field
- Setter field
- Constructors of a class
- Comments for overriding methods
- Comments for a method
New code templates can be created in an XML file and once created, the file can be imported for use. Default templates can be found by navigating to the Project Menu and then selecting Properties. In the Properties window, expand the Java Code Style and select Code Templates.
Let’s use Getter and Setter as an example and see how templates generate code. A shortcut key (Ctrl+Shift+S) can be used to open the Source Menu or alternatively you can click on Source Menu. Select ‘Generate Getters and Setters…’
A window will pop up with the Getters and Setters options. Select OK to generate the code.
The highlighted section is generated code from the Code Template feature.
Formatter is a great option to achieve a consistent format. When working on a team, Formatter helps avoid whitespace changes and code readability is greatly improved.
To enable and/or change the default format, navigate to Project–> Properties–> Java Code Style–> Formatter.
The highlighted code below is not formatted.
When Formatter is enabled, the format will be automatically corrected based on Active Profile when the changes are saved.
Clean Up is used to remove unnecessary code in a class. Clean Up can be enabled in the Properties files as shown below.
For example, Clean Up can be configured to remove unused imports.
Unused import is deleted by the Clean Up process.
Save Actions can be set up to take place every time a file is saved in the editor. It provides an option for formatting and uses the configuration from Formatter, which was covered in earlier sections. Save Actions also offer the option for code clean up and a variety of rules can be configured in Save Actions. To enable Save Actions, navigate to Properties window.
When a method or field is selected and you selected Find References, Eclipse will display the list of classes and files that uses the method or the field in the search view. This can be accomplished by using Ctrl+Shift+g. Find Reference is helpful when working on a large project or in multiple member, team setting.
If your task at hand is maintaining code or implementing improvements, it is essential to understand why and where the methods are used before making modification. Furthermore, it is important to verify that the method or the field is not being used elsewhere. If other references are found, make sure any change in method will not break existing code.
Go To Declaration
Go To Declaration helps track code and is particularly helpful during debugging or when trying to understand existing code. You can track what values are being set and what values are being passed to the method.
This is one of the common features across IDEs and the shortcut for Go To Declaration is F3. By selecting the method or the field and utilizing the F3 shortcut, the declaration of the method or the field (variable) will be displayed.
Show Annotation and Show History
The versioning repository system in Eclipse provides a great way to synchronize code from multiple developers. Quite often developers work in the same file and on the same piece of code. If a developer neglects to merge code correctly before committing his or her code into the repository, portions of code may suffer errors.
Reviewing the history, via Show History, of the file provides an audit trail to pinpoint the changes and the person who made the change. The key information from the audit trail is the developers name as it allows you to discuss the modifications with him or her. The open dialogue often is a win-win scenario in understanding the requirements, current issues, and preventing further issues.
Show Annotations is different from Show History as Show Annotations provides a detailed line-by-line annotation of the last change. Should you only be interested in reviewing changes of a line of code, Show Annotations is a more efficient way (vs. Show History) to pinpoint a change in the code.
When introduced to new IDE, take some time to explore. This can save you some time in long run and increase your comfort level and productivity. Make sure you get the most benefits out of your IDE and I hope the few tips provided in this blog will help. Please feel free to comment other shortcuts or command that you have found helpful over the time.
— Jinal Patel, firstname.lastname@example.org