Code Templates in Eclipse IDE: How it works?

How it works? is our exclusive series of posts which will focus on tricky but useful concepts / features in Java SE or in its ecosystem. The purpose is to bring into light any important concepts/tricks which are immensely useful if you learn to use it properly. These tips serve as a quick refresher for learners who have already come ahead a long way in their Java journey. Today, we are starting with Code Templates in Eclipse IDE.


I love Java, no two ways about it in my mind! I have been coding and solving problems in Java since the early days of my engineering college. But there were times I hated it – times when I felt it was so verbose. Everything has typed down and some built-in class and method names are so long. If you feel the same way, keep reading this post to learn about ways to supercharge your typing speed with the help of Code Templates in Eclipse IDE.

Keeping aside the hideous purple logo, Eclipse is a smart IDE. In many cases, you don’t need to write a lot of code by yourself. You can use the IDE for development purposes only and use another tool which uses UML for designing the architecture of your application.

This is made possible by a feature called Code Templates. In this post, I will try to shed some light on how you can improve your coding speed with the help of pre-made code templates in Eclipse IDE.

What is a Code Template?

A code template is what it exactly sounds like – it’s template of code which can be reused. When you are coding for a project in Java, you  come across many repetitive structures. For example, you might use loops a lot if you are working with collections. If you are working with Servlets, you might need to read and convert parameters. You might use request.getParameter() method frequently.

A code template is a built-in fragment of code (with dummy variable names) for such repetitive coding tasks.

Ever heard of Content Assist in Eclipse IDE?

If you have ever used CTRL + Space shortcut key combo, that is exactly what I’m talking about; Content Assist is just a fancy name for it!

Basically, Eclipse pulls out an auto-complete list whenever you press CTRL + Space shortcut. We can use this in combination with built-in code templates. Let’s see how to do it.

Using Existing Code Templates in Eclipse

To check the built-in code templates first, follow the below path of commands:

“Window” menu > “Preferences” option > “Java” menu > Editors > Templates

In this window, you will see a lot of built-in templates which will be for Java statements, SWT statements, or Javadoc comments. The window looks like this:

Eclipse Preferences

Eclipse Preferences

 

To use any of these templates, just type their name and press CTRL + Space in your Java file. This opens the Content Assist menu.

As you can see in this above window, Eclipse already has built-in for-loop templates with the name “for”. To use them, just type “for” and press CTRL + Space, the content assist menu will popup from which you can select the appropriate template based on your need.

Sample of Existing FOR LOOP template in Eclipse IDE

Sample of Existing FOR LOOP template in Eclipse IDE

 

How to create your own code templates

When you open the Code Templates window, you will see a “New” button to create a custom code template.

The steps for creating custom code templates in Eclipse IDE is:

  • Click “New” button on the Templates tab of Eclipse Preferences window. A “New Template” dialog box is shown as in the picture below:
Creating a New Code Template in Eclipse

Creating a New Code Template in Eclipse

 

  • Give the template a name and a description.
  • In the pattern text box, click “Insert Variable” button and select a variable of your choice. You will define a pattern using these built-in variables and click OK when you’re done.

Now your template is ready to be invoked in code.

Example of Code Template in Eclipse IDE

Now that you have seen the process of creating code templates in Eclipse IDE, let’s try creating one with an example.

In this example for code templates in Eclipse, we will create a template for printing an exception in a catch block with a custom message.

Follow the steps given above and open the “New Template” dialog box. The values of the fields will be as given in this image.

Code Template Example in Eclipse IDE

Code Template Example in Eclipse IDE

Notice the usage of the exception variable, It is written with a “$” symbol; this is a special syntax used by Eclipse to identify  different types of object when the template is invoked at the time of coding.

So, in this post, we learnt one of the most important time-saving features of Eclipse. If you create your own code templates that can fasten your coding process, do let me know in comments.


If you want learn Java so that you never forget it, you can follow us on Twitter or like us on Facebook for regular updates.

 

 

Leave a Reply