Eclipse tutoria...login
Eclipse tutorial
author:php.cn  update time:2022-04-13 14:09:28

Eclipse code templates


Using code templates

Eclipse provides the ability to improve work efficiency and code predictability by defining and using code templates.

In the process of developing Java programs, we often need to write the main method:

public static void main(String[]args) {

}

If we write it letter by letter, it will be a repetitive and meaningless thing. This is what we You can use Eclipse code templates to quickly complete these tasks.

We only need to type main in the class body, and then use Eclipse's code prompt shortcut key (the default is Alt+/). After pressing Enter, you can see that Eclipse automatically helps us complete the complete definition of the main function. :

tmp1

If we want to use System.out.println(), we only need to enter syso and press Alt+/:

tmp2

Custom code Template

Eclipse also provides a lot of code templates, which we can see through Windows->Preferences->Java->Editor->Templates (you can enter Templates in the search box to find) List of all defined code templates.

tmp3

We select the sysout template in the pop-up window and click Edit on the right, which is displayed as follows:

tmp4

The editing panel is the core object of attention, because everything is configured here. First, let’s get familiar with what the five key items in this panel are.

  • Name: The name is actually the code abbreviation that can be used in the future

  • Context: Template context, specifies where the code template can take effect. For Java, it contains at least four:

    1. Java type members, the code corresponding to the template is a class member, psvm Strictly speaking, the template should choose this

    2. Java statements. The code corresponding to the template is the statement block

    3. Java. The most common one is as long as it is Java Just code

    4. Java doc, as the name suggests

  • Template variables:eclipse has preset some templates Variables (click Insert Variables to see all preset variables), such as: Of course, we can also define our own template variables. For example, if I define a ${myTemplateVarible}, then the corresponding code displays myTemplateVarible.

    1. ${cursor} represents the cursor

    2. ${date} represents the current date string

    3. ${time} represents the current time string

    4. ${line_selection}Let the current line be selected

    5. ${word_selection}Let the current word be selected

  • Pattern:The pattern corresponding to the code template, just enter it one by one according to the format you want the code to be in.

You can customize the code template for more content By clicking the Help Contents option in the Help menu, enter "Java Editor Template Variables" in the search bar of the pop-up dialog box. Select Java Editor Template Variables to view the specific document description:

tmp5

php.cn