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:
If we're going to use System.out.println(), we just need to enter syso and press Alt./
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).
We select the sysout template in the impeachment window and click on Theit on the right, which shows the following:
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:
- Java type members, the code corresponding to the template is a class member, psvm template should strictly choose this
- Java statements, the code for the template is a statement block
- Java, the most common, as long as it's Java code
- Java doc, as the name suggests
-
Template variables:
eclipse has preset some template variables (point Insert Varibles can see all preset variables), such as:
- $(cursor) is the cursor
- $'date' represents the current date string
- $'time' represents the current time string
- $(line_selection) to have the current row selected
- $word_selection the current word is selected
- 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: