Latest web development tutorials

Eclipse code templates

Using Code Templates

Eclipse provided by defining and using code templates to improve the efficiency and predictability of the code capability.

We often need to write Java programs in the development process, main methods:

public static void main(String[]args) {

}

If we are to write a letter a letter, but will be a repeat meaningless things, which we can use Eclipse code templates to quickly accomplish these tasks.

We just type in the main body of the class, and then use the Eclipse code prompt shortcut (default Alt + /), press Enter, and you can see Eclipse automatically help us to complete the full definition of the main function:

tmp1

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

tmp2

Custom code templates

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

tmp3

We select the playing window sysout template and click on the right Edit, appears as follows:

tmp4

Edit panel is the core concern, because everything in this configuration inside. Let's familiar with this panel key five are what.

  • Name: The name, in fact, it can later be used by the code abbreviations
  • Context: template context, specify the code template where to take effect, so for Java contains at least four:
    1. Java type members, the corresponding code template is a class member, psvm strictly speaking, you should choose the template
    2. Java statements, the corresponding template code is a block of statements
    3. Java, the most common, as long as the Java code on the line
    4. Java doc, as the name suggests the
  • Template variables: eclipse has a number of preset template variables (point Insert Varibles can see all the preset variables), such as:
    1. $ {Cursor} is a cursor
    2. $ {Date} represents the current date string
    3. $ {Time} string representing the current time
    4. $ {Line_selection} so that the current row is selected
    5. $ {Word_selection} let the current word is selected
    Of course, we can also define your own template variables, for example, I define a $ {myTemplateVarible}, then the corresponding code is displayed is myTemplateVarible.
  • Pattern: code templates corresponding mode, as you want to format the code to enter one by one

More custom code template content you can click Help menu Help Contents option in the search field pop-up dialog box, enter "Java Editor Template Variables" select Java Editor Template Variables view specific documents describe:

tmp5