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.
The Quick Outline is basically a scaled-down popup version of the Outline View. The main advantage over the Outline View is that it has a search box that allows you to search for a method.
Here’s how to use the Quick Outline:
- Press Ctrl+O from anywhere within the class.
- Type a search term in the search box and Eclipse will just show all methods that match the search term. By default Eclipse does an exact match search, but you can use wildcards.
- Once you see the method you’re interested in, press Down to select the method (if it’s not selected already).
- Press Enter once the method is selected. Eclipse will take you directly to the method’s declaration.
This is an example of the popup for Java’s ArrayList.
- Sort the view using the the down arrow menu in the top right corner. It makes it easier to find methods by scanning.
- Resize the Quick Outline popup to see more methods. Eclipse remembers the size of the popup for the next time you open it.
Next Member & Previous Member
Another way to move between methods is to use two features called Go To Next Member and Go To Previous Member.
When you press Ctrl+Shift+Down, Eclipse moves the cursor to the next method in the class. Pressing Ctrl+Shift+Up moves to the previous method.
Here’s a video to give you a quick example of how these shortcuts work:
This shortcut works best if you’re already positioned in a method or if the class has few fields. That’s because to Eclipse a member can either be a method or a field. If you’re at the top of the class, you have to move through all the fields before you actually start moving through the methods themselves, a time-consuming process especially for bigger classes with lots of fields.
It helps to generate getters and setters at the bottom of the class because you don’t have to navigate through them to get to the useful methods.
If you’ve got lots of private methods in your class then Open Declaration might be the best way to navigate between them.
When you’re positioned on a method call and press F3, Eclipse takes you directly to the definition of that method. For example, if you’re busy in the method process() and your cursor’s positioned on initProcessing(), pressing F3 will take you directly to the declaration for that method further down the class.
This feature works very well with Alt+Left (Backward History). See the section below for more details about the Backward History.
Navigating Back to a Previously Viewed Method
While browsing code, you’ll often want to return to the previous method you were viewing once you’re done viewing the method it calls.
To do this, use Alt+Left (Backward History) to move back to the last navigation point. This feature isn’t specific to just methods, but also works for navigating between previously visited editors. But it works great if you’ve just been browsing methods within a class.
- Eclipse’s Outline View also allows easy navigation but mostly with the mouse. You could navigate to the view using Alt+Shift+Q, O, and you can move to methods by typing their first letter, but I’ve found the Quick Outline to be more keyboard friendly. Also, the Outline View doesn’t support wildcard searches.
- You can also use Eclipse’s Call Hierarchy (Ctrl+Alt+H) especially if you’re trying to understand the flow of methods in a class and move between them easily. Knowing how to navigate between views and editors with the keyboardhelps a lot since you’ll be moving between the Call Hierarchy view and editors a lot.
What Should I Use When?
- Use Next/Previous Member shortcut if the class is small or you have a good idea of where other methods are in relation to the current method (eg. are they above/below the current method).
- Use the Quick Outline if you don’t know the class too well or there are lots of methods in the class.
- Use Open Declaration if you’re moving between many private methods of the class. It’s normally the fastest way to move to another private method in the class, but only if you’re already positioned in a method that calls it.