Top 10 Java tips

Use Eclipse Oxygen

There are many improvements in the last release of Eclipse alone, and with the latest build released just a few days ago, the IDE supports Java 9 out of the box. Some of these improvements are:

  • Showing the last returned value while debugging a Java program. This shows you the value returned by the last method after stepping through the program statements.
  • Better Java 8 support, with numerous bug fixes for lambda expression type inference.
  • A new Java index that significantly improves tooling performance, such as when loading the type hierarchy of platform classes and interfaces.
  • Support for JUnit 5. You can find a list of Java tooling improvements in Oxygen here.

Switch Editors Using Ctrl+Tab

If you're used to switching tabs in browsers and editors like Notepad++, then do yourself a favor and change the keybinding for switching between Java source files. Go to Window -> Preferences -> Keys, then search for "Next editor" and "Previous editor", and override their bindings to Ctrl+Tab and Ctrl+Shift+Tab respectively (note: I'm using Windows).

Group Related Projects in Working Sets Before Choosing Multiple Workspaces

If you work on many different projects, at some point you may need to use multiple workspaces to separate them. But before reaching that level, you can just group related projects into a working set. This way you don't have to switch workspaces or have two Eclipse windows using different workspaces. It also keeps your projects organized and accessible from within the same view. For example, I typically keep a working set for sample projects for quick experimentation, and then typically a working set for each group of related modules in a Maven project. One thing that you may need to do is change the Package Explorer view to view these working sets as shown below. I often end up with many working sets containing projects I'm not using. In this case, I can simply close a working set by right-clicking on it from the Package Explorer and selecting Close Project. This reduces memory consumption in the IDE and makes it as if these closed projects do not exist in your workspace anymore until you re-open them. Only when I have too many working sets, or I have projects that considerably differ from each other that I rarely switch between, then I separate them in different workspaces.

Set The "Incremental" Option in the Search Dialog

When you hit Ctrl+F to find text in a source file, check the Incremental checkbox in the search dialog to make the occurrence of the searched text appear as you type. This seemingly minor detail helps me avoid typing too many characters and then hitting return to find what I want.

Use Navigation and Search Shortcuts

Here are a few shortcuts to help to understand your code (using Windows). These are so useful that eventually, they'll easily become second nature:

  • F3 or Ctrl+Left click: goes to declaration of element
  • Ctrl+T: view type hierarchy and implementation methods
  • Ctrl+Alt+H: view call hierarchy of selected element
  • Ctrl+Shift+G: search workspace for all references to selected element
  • Ctrl+Shift+T: search for a class, interface or enum
  • Ctrl+Shift+R: search for a resource (e.g. text file)

Use the File Search Feature

This is really helpful if you want to search files in your workspace for text. The search can be filtered by file type and scope of the search, such as searching only the selected project.

Use Ctrl+Space and Ctrl+1 for Content Assist and Quick Fixes

Ctrl+Space allows for auto-completion. It can also be used to override methods and generate getters /setters and constructors. Ctrl+1 can be very handy in quick and smart fixes such as:

  • Assigning constructor parameters to new or existing class fields
  • Assigning a statement to a local variable
  • Renaming a variable, field or class, etc.

Use Code Generation Actions

Alt+Shift+S can be used to quickly add common code:

  • Generating getters/setters and constructors
  • Overriding hashCode() and equals()
  • Overriding toString()

Ctrl+3 Is Your Friend

As with any modern IDE, there are many keybindings to do all sorts of actions in Eclipse. But the most important is probably Ctrl+3 for Quick Access which is an entry point to virtually all actions you can do. I typically use it to open a view, but it can also be used to do refactoring, creating a new project, and lots of others.

Download the Sources of Libraries

If you're using Maven, you can download the source code of your dependencies. There is preference under Window -> Preferences -> Maven that when selected automatically fetches the source artifacts of dependencies. You can also download sources manually by right-clicking on the dependency in the Maven Dependencies tree and selecting Maven -> Download Sources. Usually, this also makes Javadoc comments available in your IDE when you hit F2, so you no longer need to browse it separately. There is a similar way to do this in Gradle.