Some Shortcut Tips for Eclipse for Java Devs

#1 Shortcut Tips for Using Eclipse

Here are some shortcuts in Eclipse for Java developers.  It took me many years to gather these shortcuts, and I have not seen them described elsewhere.  Other Java Devs have been loath to share these, but I am not.  I have found them to be absolutely necessary in my day-to-day functioning as a Java Dev.  By learning these shortcuts, you wont be able to keep up with an IntelliJ developer, but at least you will feel like you can.  Please be a cool person and share these tips with others.

>> “CTRL-SPACE”

If you click on an empty line and do this command, you get a list of fields and methods you could call, and a list of commands that will generate boilerplate code for common tasks in Java.  On the other hand, if you type out one of these boilerplate-generating commands and then hit CTRL-SPACE, the boilerplate code will replace the command you wrote out with the code.  So, now you have 2 ways to access this boilerplate code.  For instance, if you type “sysout” and hit CTRL-SPACE, “sysout” is replaced with “System.out.println()”.  Others include “if”, “elseif”, “else”, “for”, “foreach”, “synchronized”, “try_catch” and more.

Also, if you type out an object reference name, and click “.” following it, you automatically get a list of methods that can be called on the object, where as you type out the method name characters, the listing automatically narrows down to the fewer options that contain the letters you have typed so far.  Sometimes you can make a mistake typing, where the autocomplete feature becomes empty when no method names match the misstyped characters.  In the past I would always delete the entire method name, delete the period, and start over again.  With CTRL-SPACE, you don’t need to start over again completely.  Instead, delete the misstyped chars, and hit CTRL-SPACE, and the autocomplete is reactivated for you.

Another way to use CTRL-SPACE, but via a menu option, is to go to Edit > Content Assist.  There you have a list of “proposals” of different domains of Java code, including Types, JAXB, JAX-WS, JPA and more.

>> “CTRL-1”

This is called the “Quick Fix” option.  If you start typing some code in a line, for instance calling say “BuilderFactory.newInstance()”, but you have not yet typed out the local variable to assign this to, typing CTRL-1 will open a menu of options.  These include “create a local variable”, “create a field”, and so on, where the suggested boilerplate code you could use is shown to the right of the selection name.  This feature is pretty powerful, so whenever you start writing out a line of code, you should think about using CTRL-1 to see what Eclipse suggests for you to use.  I just learned about this command so I will be investigating its use with much interest.  This can also be found in a menu, via Edit > Quick Fix.

>> “ALT /”

Not as important as CTRL-SPACE or CTRL-1, ALT / is the “Word Completion” shortcut.  Start typing a word, and before finishing, hit ALT /.  Eclipse will complete the word for you, in the same style as you expect when hitting tab on the command line in Windows or Linux.  This can also be found in a menu, via Edit > Word Completion.

>> “CTRL-Shift-O”

This does “organize imports”.  This means that you can write code, have the real-time syntax checker highlight your code, and then when you do CTRL-Shift-O the needed imports will be automatically inserted for you.  If there is ambiguity, say because there are 2 classes with the same name, you just have to pick the one you want.  i.e.: java.util.List, or java.awt.List.  NOTE: the relevant jar files will need to be in your class path for this to work.

>> Hover over class or interface, and click “CTRL”

Have you seen an interface used that you wanted to know what the implementation for?  Hover + CTRL will popup a list of “Open Declaration”, “Open Implementation” (and “Open Return Type” for methods).  When you select “Open Implementation” you are taken to the correct class that implements the interface/class/method.  This is much easier than wading through a huge project or doing a File > Search.

>> Highlight a method name and hit F3

Do you want to go directly to the implementation of a method?  This does that, the class where the method resides is opened for viewing.

>> CTRL-Shift-H

“Show in class hierarchy”  Type in the name of a class, your own or residing in a jar file, and the inheritance hierarchy of the class is shown in a file explorer-type window.  NOTE: You can then double click on the class to view it (the class is not shown for viewing to start with).  NOTE: Typing out the class is done with an autocomplete feature, which is nice.

>> CTRL-Shift-T

“Show class type”  Type in the name of a class, and the class is shown to you for viewing.  Like CTRL-Shift-H but with no inheritance hierarchy shown.  NOTE: This has an autocomplete feature.

>> CTRL-Shift-R

“Show resources”  The same as “CTRL-Shift-T” but includes files that are not classes, like .txt, .json, etc.  NOTE: Also has autocomplete.

>> F4

Basically the same as CTRL-Shift-H.

>> CTRL-ALT-H

Select a method and do CTRL-ALT-H, and the chain of method calls leading to this method is shown in a File-Explorer-type view.  Top of the hierarchy is the end of the calls and the bottom is the start of the calls (same order as you see in a stack trace).

>> “+” button and “-” button

In many of the view and explorer type windows, there is a “-” button and sometimes and “+” button.  Clicking the “+” button will open all entries in the view, down to the class level, starting at the selected entry on down.  The “-” button collapses the entries in the exact same way.  This is handy because drilling down in a view like this, one entry at a time is very laggy (to use a Minecraft term) in Eclipse.

>> “numpad add, subtract, and multiply”

On the number pad, whether or not when “num lock” is selected, “*” and “+” will drill down into Package Explorer to open / expand the hierarchy completely, like the “+ expand” button does.  This is handy because the Package Explorer doesn’t have the “+ expand” button.  Also, “*” button is like the “+” button, but it opens the entries down to the method and field level.  For me personally, this is less handy.  Not to be left out, “-” acts just like the “- collapse” button does.

>> “Double Arrow”

In the Package Explorer, there is a button that shows the top arrow pointing left (to the Package Explorer) and the bottom arrow to the right (to the class files).  Toggling this button “on” means that whenever you select a class or file to view, the corresponding entry in the Package Explorer will be highlighted.  The two are “linked” in a way.  This is handy when you want to see the files organized nearby the class or file you currently have selected for viewing.  Also, if you have a ton of projects in a workspace and a ton of open files, this helps you find the file in among the projects right away, saving you precious time.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s