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. :
If we want to use System.out.println(), we only need to enter syso and press Alt+/:
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.
We select the sysout template in the pop-up window and click Edit on the right, which is displayed as follows:
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:
Java type members, the code corresponding to the template is a class member, psvm Strictly speaking, the template should choose this
Java statements. The code corresponding to the template is the statement block
Java. The most common one is as long as it is Java Just code
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.
${cursor} represents the cursor
${date} represents the current date string
${time} represents the current time string
${line_selection}Let the current line be selected
${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: