More code templates in Eclipse

How about this…

Start typing the name of a method (where a method should go). Don’t worry about the return type or anything else, just the name. Hit ctrl-space. Eclipse will offer to build a new private method with that name. Type “main” and hit ctrl-space and it will build you a new main method stub (complete with all the necessary parameters and return types).

Or… highlight a few lines of code in an existing method and hit ctrl-space. Down near the bottom of suggestions are a few interesting ones, including “do”, “for”, “if”, “runnable”, and others. Select one of these and it will wrap the selected code with a “do” loop, “for” loop, and so on. “Runnable is my favourite; it wraps the code in a new instance of java.lang.Runnable instance.

So…


private void whatDoYouGetWhenYouMultiplySixByNine() {
doSomething();
doSomethingElse();
}

becomes…


private void whatDoYouGetWhenYouMultiplySixByNine() {
new Runnable() {
public void run() {
doSomething();
doSomethingElse();
}
}
}

The best part is that if you look at how these templates are defined, it’s pretty easy to sort out how to define your own. More on that later…

This entry was posted in Uncategorized. Bookmark the permalink.

One Response to More code templates in Eclipse

  1. Philipp says:

    Hi Wayne,great Tip with the “runnable”. I didnt know that. Philipp

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s