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.

Keyboard shortcuts for comments and JavaDocs

Here are the keyboard shortcuts for manipulating comments.

Command Description
Ctrl+/ Toggle Comment Add/remove line comments (//…) from the current line. The position of the cursor can be anywhere on the line. Works with multiple selected lines as well.Alternatively, you can also use Ctrl+Shift+C, which probably works better on a QWERTZ keyboard (eg. German layout), or Ctrl+7.
Ctrl+Shift+/ Add Block Comment Wrap the selected lines in a block comment (/*… */).
Ctrl+Shift+/ Remove Block Comment Remove a block comment (/*… */) surrounding the selected lines.
Alt+Shift+J Add Javadoc Comment Add a Javadoc comment to the active field/method/class. See the notes below for more details on where to position the cursor.

Bear the following in mind when using Add Javadoc comment (Alt+Shift+J):

  • To add a comment to a field, position the cursor on the field declaration.
  • To add a comment to a method, position the cursor anywhere in the method or on its declaration.
  • To add a comment to a class, the easiest is to position the cursor on the class declaration. Also works if you’re in the class, but not in a method, field or nested type.
  • The Javadoc comment inserted is based on the Code Templates defined  under Window > Preferences > Java > Code Style > Code Templates. If you expand the Comments section, you can change the default for Fields, Methods, Types (eg. classes), etc.

Here’s a video to give you an idea of how fast and easy it is to add/remove comments using these shortcuts. The video shows toggling of single line comments, block comments and also adding a Javadoc comment to the method and class.

Once I’ve commented out lines, I often find myself copying them and moving them around (eg. to try different variations of the code). You can do this faster by moving and copying lines using with a single keystroke.

You can also have Eclipse format the comments whenever you save, saving you formatting time.

Related Tips

Share This Tip

Support Eclipse On E

6 thoughts on “Add comments and Javadocs in Eclipse with a single keystroke

  1. Post updated thanks to Sancar Gazi at DZone who pointed out that Ctrl+Shift+C is easier on QWERTZ keyboard (eg. German layout) than Ctrl+/ (which is above the 7).

  2. Nice video. I like the concept of displaying keystrokes in semi-transparent captions at the bottom. I just tried Alt+Shift+J for a method with a parameter and Eclipse was smart enough to insert @param someparameter in the comments block.