Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Eclipse code template


May 24, 2021 Eclipse


Table of contents


Use code templates

Eclipse provides the ability to increase productivity and code predictability by defining and using code templates.

We often need to write the main method during the development of Java programs:

public static void main(String[]args) {

}

If we write one letter at a time, it's going to be a repetitive and pointless thing, and that's what we can do quickly with eclipse code templates.

All we have to do is type the main in the class body, and then use Eclipse's code prompt shortcut (alt-plus/), and when we enter, we can see that Eclipse automatically helps us complete the full definition of the main function:

Eclipse code template


If we're going to use System.out.println(), we just need to enter syso and press Alt./

Eclipse code template

Custom code templates

Eclipse also offers a very large number of code templates, and we can see a list of all defined code templates through Windows-Preferences-Java-Editor-Templates ( you can enter Temples in the search box to find them).

Eclipse code template

We select the sysout template in the impeachment window and click on Theit on the right, which shows the following:

Eclipse code template

The editing panel is the core focus because everything is configured here. Let's familiarize ourselves with the five key items in this panel.

  • Name: The name, in fact, is the code abbreviation that you can use later
  • Context: The template context, which specifies where the code template will take effect, contains at least four for Java:
    1. Java type members, the code corresponding to the template is a class member, psvm template should strictly choose this
    2. Java statements, the code for the template is a statement block
    3. Java, the most common, as long as it's Java code
    4. Java doc, as the name suggests
  • Template variables: eclipse has preset some template variables (point Insert Varibles can see all preset variables), such as:
    1. $(cursor) is the cursor
    2. $'date' represents the current date string
    3. $'time' represents the current time string
    4. $(line_selection) to have the current row selected
    5. $word_selection the current word is selected
    Of course we can also define our own template variables, for example, if I define a $(myTemplateVarible) and the corresponding code shows myTemplateVarible.
  • Pattern: The pattern for the code template, enter it one by one in the format you want the code to be

For more custom code templates, you can select Java Editor Variable Templates to view the specific document description by clicking on the Help Contents option in the Help menu and entering "Java Editor Template Variables" on the search bar of the pop-up dialog box:

Eclipse code template