The easiest ways to navigate methods in a class using Eclipse keyboard shortcuts

Java classes can get big and hairy, making it difficult to find the method you’re looking for when browsing or editing a class. There is no specific order to where methods can be in a class and different developers have different preferences about where to put them.

You could use the mouse wheel and scroll ferociously until you eventually find the method or you could even use Page Down/Page Up on your keyboard. But these methods can be time-consuming and haphazard, especially when the class has lots of methods or they’re scattered in an arbitrary order.

Luckily, Eclipse has a number of fast and easy ways to help you navigate methods in a class, especially using the keyboard. I’ll discuss some of those keyboard shortcuts and also which ones to use when.

Continue reading

Add comments and Javadocs in Eclipse with a single keystroke

When you want to work with comments in Eclipse, you could use the slow way of moving to the start of the line, pressing // and then repeating this for all the lines you have.

Or you could use the quick way of adding a comment with a single keystroke no matter where the cursor’s positioned in the statement.

The same goes for Javadocs – there are just too many things to type before you can start commenting the good stuff. That’s why Eclipse also has a shortcut that let’s you add Javadoc to a field, method or class.

Continue reading

Generate class constructors in Eclipse based on fields or superclass constructors

You’ll often need to add a constructor to a class based on some/all of its fields or even based on constructors of its superclass. Take the following code:

public class Contact {
    private String name, surname;
    private int age;

    public Contact(String name, String surname, int age) { = name;
        this.surname = surname;
        this.age = age;

That’s 5 lines of code (lines 5-9) just to have a constructor. You could write them all by hand, but writing a constructor that accepts and initialises each field takes a lot of time and becomes irritating after a while. And creating constructors from a superclass can take even longer because the superclass can define multiple constructors that you need to reimplement.

That is why Eclipse has two features to help you generate these constructor instantly: Generate Constructor using Field and Generate Constructor from Superclass. Both features will generate a constructor in seconds, freeing you up to get to the exciting code. You’ll also see how to add/remove/reorder arguments of an existing constructor based on fields defined in the class.

Continue reading

How to manage keyboard shortcuts in Eclipse and why you should

When I use Eclipse I try and use shortcuts all the time. They really make me work faster and give me time to focus on getting the code out rather than slowing down to invoke IDE commands with the mouse.

Because people work differently and on different things, it’s important to manage keyboard shortcuts to suit the way you work. So it’s good that Eclipse makes it really easy to change shortcuts and also to view shortcuts for the commands you use a lot.

I’ll discuss how to change keyboard shortcuts, how to avoid conflicts with existing ones, why it’s a good thing to manage shortcuts and then end off with some examples of common shortcuts that you should add to you arsenal. Continue reading

Generate static imports in Eclipse on autocomplete for JUnit Assert, StringUtils and others

Eclipse normally adds import statements for you when you autocomplete a type, press Ctrl+Shift+O or organise imports when you save. However, by default it doesn’t do the same for static imports, something you’ll miss when using classes like JUnit’s Assert and Apache Commons’ StringUtils. This means having to add import statements manually, which takes unnecessary time.

So, in the code below, I want to be able to autocomplete assertEquals (line 7) and have Eclipse add a static import for org.junit.Assert.assertEquals (line 1).

import static org.junit.Assert.assertEquals;
public class MyTest {
   public void testSomething() throws Exception {
      assertEquals(1, 1);

Now, the good news is that there is a way to automatically add static imports when you use autocomplete. It’s done via a preference that tells Eclipse which static classes you want to include for Content Assist (autocomplete). I show you how to do this below.

The bad news is that there doesn’t seem to be a way to tell the Organise Imports command to add static imports. However, a working autocomplete should already cater for 90% of your coding needs, so the bad news isn’t that bad.

Continue reading

Run/debug a class in Eclipse as a JUnit test or Java app with a single keystroke

If you’ve used Eclipse for a while, you may have run into the commands Run As and Debug As. These are used to launch a class/project as a particular application, eg. a Java application, JUnit test or Eclipse application.

They’re normally accessed with the mouse via the right-click menu or the pulldown menu on the run/debug icons in the toolbar. However, using the mouse to launch these classes isn’t the fastest way of doing things, especially if you’re frequently running different classes, such as JUnit tests.

To help with this, Eclipse has keyboard shortcuts to invoke the Run As/Debug As commands. They really speed up launching apps (which you’ll do often in a day’s work). I’ll show you these shortcuts and also a couple of others to make life easier.

Continue reading

Instantly show a class/file in the Package/Project Explorer in Eclipse

While working on a class or XML file, you’ll often find that you want to work with the actual .class or .xml file in the Package Explorer, Project Explorer or Navigator views. You may want to copy, move or delete the file or do stuff that are only accessible in these views (eg. browse all files in the same folder).

Eclipse has a feature called Show In, that’s almost like a Quick Open. It instantly navigates to the current editor’s file in a requested view and positions you directly on the file in that view, no matter how deep in the hierarchy the file is. It’s also keyboard friendly, which means no more reaching for the mouse.

This feature is perfect for those (like me) who don’t like the Link with Editor functionality on certain views. I don’t like the Package Explorer to Link with Editor as I like to know things are the way I left them (see the rant at the end of the post).

Continue reading

Split and view the same editor side by side in Eclipse

At some point you’ll want to view the same class or file side by side. You may want to follow related code in different parts of the class or need some code constantly available as reference to change some other part of the class.

Eclipse allows you to split an editor and move it to anywhere in the editor area in the same window, including next to the original editor. Changes made in the one editor are reflected in the other. The feature isn’t very obviously named, but it is easy to use.

Continue reading

Automatically format and cleanup code every time you save

Eclipse has a wonderful feature that formats according to your coding standards. It handles things like indentation, where curly braces are placed, if blank lines should occur between field declaration and hundreds of other options.

However, to invoke this formatting, you have to tell Eclipse to do this every time you’ve edited the code. You can do this using Ctrl+Shift+F but (1) you have to do that every time and (2) you have to remember to do it in the first place (and we all know how good developers are at remembering things).

Eclipse sports a feature called Save Actions that makes formatting code easy. With this feature enabled, every time you save the file, Eclipse formats the code to your formatting preferences and does some cleanup of the code (eg. remove unused imports). All done automatically and for free. Continue reading

Move, copy and delete lines with a single keystroke

When coding new features or making big changes to existing functions, I end up moving around a lot of code by typing something, deleting it, copying another line to modify it slightly, moving another line up or down, etc. Deleting a line is also more common than you think.

Instead of selecting lines and using standard copy, paste and delete to copy, move or delete lines, I’d rather use only single keystrokes. Why? Because selecting a line is slow since you have to move the beginning to fully select it. Then you have to copy, move to the new location and paste. Also, it impresses any colleagues watching me code.

Continue reading