Generating Code Automatically Using Custom code Template In Eclipse

One of the good features of Eclipse is its auto fill property. This can be further enhanced using its code template feature. Creation of code templates is necessary to improve development productivity. Templates add consistency and uniformity to your code. Eclipse has so many ready-to-use templates. And you can create your own code templates according to your requirements. To use these templates just type the starting characters of the template and then press “CTRL+SPACE”. For example in Eclipse IDE type “tr” and then press “CTRL+SPACE”, It will add “try-catch” block to the code.

This article will guide you to create your own code template. Let us take an example to generate a “if-else” template with one “else if” condition.

  1. Go to the Templates ( Window –> Preference –> Java –> Editor –> Templates) .
  2. Click “New”. A window similiar to below appears.
  3. 1.JPG

  4. Fill the details as shown above.
  5. Click “OK”.
  6. Now you can use your custom template in the program like any other template by pressing “CTRL+SPACE” as shown in the below given screenshots.