The Eclipse code template uses code templates
Eclipse provides the ability to define and use code templates to improve productivity and code predictability.
We often need to write the main method in the process of developing Java programs:
public static void Main (String[]args) {}
If we write one letter at a letter, it will be a repetitive and meaningless thing, and this is where we can use the Eclipse code template to do this quickly.
We just type main in the class body and then use Eclipse's code hint shortcut (default = alt+/), and when you enter, you can see that eclipse automatically helps us complete the definition of the main function:
If we want to use SYSTEM.OUT.PRINTLN (), we just need to enter SYSO and press alt+/:
Custom code Templates
Eclipse also provides a lot of code templates that we can get through windows->preferences->java->editor->templates (you can enter Templates find in the search box) See a list of all defined code templates.
We select the Sysout template in the pop-up window and click on the right edit to display the following:
The edit panel is the core concern because everything is configured in this area. First, familiarize yourself with what the key five items in this panel are.
- Name : In fact, it is the code abbreviation that can be used later
- Context : template contexts, specifying where the code template will take effect, and for Java at least four of these:
- Java type members, the template corresponding to the code is a class member, PSVM template Strictly speaking should choose this
- Java statements, the code corresponding to the template is a statement block
- Java, the most common, as long as the Java code on the line
- Java Doc, as the name implies
- template variables:eclipse has preset some template variables (point insert varibles can see all preset variables), such as:
- ${cursor} is a representation of the cursor
- ${date} represents the current date string
- ${time} represents the current time string
- ${line_selection} Let the current row be selected
- ${word_selection} Let the current Word be selected
Of course we can also define our own template variables, such as I define a ${mytemplatevarible}, then the corresponding code shows the mytemplatevarible.
- Pattern : Patterns for code templates, which you can enter in the format you want the code to be
For more custom code template content you can select Java Editor Template Var by clicking the Help Contents option in the Help menu and typing "Java Editor template Variables" on the search bar of the popup dialog box Iables See the specific document description:
Eclipse Code templates