Eclipse Tip: Add your own Code Template in Eclipse

If you are using Eclipse as your primary development IDE, then you may want to know about Code templates in Eclipse. Code templates are certain user defined (some are available by default) templates which can assist in rapid code writing. Not only code templates increase your code writing speed, but it also add consistency across the code. To add a code template, all you need to do is to write first character and then press CTRL + SPACE. For example, in Eclipse write tr and press ctrl+space. It will add a try-catch block in your editor.

User defined code templates

You can defined your own code templates. Just goto Windows > Preferences > Java > Editor > Templates. You can click on New.. button to add your own code template. Lets create our own code template. For this example, we will create a template which will check if a local variable is null or not, if its null then it will throw a NullPointerException. Open Template editor from above path and press New.. Then enter following details. Name: npe Description: To check if a local variable is null or not. Pattern:
if (${arg:localVar} == null) throw new ${exception:link(NullPointerException,IllegalArgumentException)}("${arg:localVar} is null");
Code language: Java (java)
And press Ok to save the template. Now go to any editor window in Eclipse and press npe and press CTRL + SPACE. You will see a suggestion window which will display you an option to select the npe code template.

View Comments

Share
Published by
Viral Patel
Tags: eclipse eclipse feature eclipse tricks

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

5 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

5 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

5 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

5 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

5 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

5 years ago