Make full use of the eclipse Code Template Function

Source: Internet
Author: User

Keywords: Eclipse Code Template



Eclipse is a very powerful ide. As a Java programmer, it deals with it almost every day. But does it use its powerful functions?

Today, let's take a look at the "Code Template" function in eclipse. What is a code template? For example, how to create a main function in a common application?

Isn't it easy? The voice hasn't fallen, and you can only hit it on the keyboard quickly.

OK. I typed it quickly. However, do you have to think about it? Is it necessary to write a letter or a letter into this very fixed code? As an IDE, one of the design philosophy is to help users do what they do. In the above case, the eclipse code template should play a role.
You only need to type main in the class body, and then use the eclipse code to prompt the shortcut key (Alt +/by default). Press enter, eclipse automatically defines the main function. Is it very convenient?
This is just a simple example. In fact, eclipse also provides many code templates by default. Choose Windows> preferences> JAVA> editor> templates to view the list of all the defined code templates.


Another more common example is to output some data system. Out. pringln ("Hello, world") on the console ");
In the past, if you were used to typing a letter or a letter, you can try typing sysout and press Alt +, in addition, the cursor is very considerate and stops in brackets. Is it very powerful for you to continue inputting?

In programming, another type of problem may occur. For example, I want to add some code to the try/Catch Block for exception detection. With the help of the Code Template, you do not need to manually type this part of the code or worry about code indentation. You only need to select the code block to be processed, and then press Alt + Shift + Z, after you press enter, eclipse automatically packs the try/catch statement for the selected code block.



Although eclipse already provides many useful templates by default, it is definitely not enough. Therefore, we need to try to create our own code template.
We use a realistic example to illustrate that in actual development, we often need to test the execution time of a code segment. In general, we will use the following methods.
Record the timestamp before and after code execution, and then calculate the difference between the two timestamps. This piece of code is very solid. If you press it manually every time, it is very crashing, but it is much more convenient to use the "Code Template" function.
Choose Windows> preferences> JAVA> editor> templates, click New..., create a code template, enter the code as shown in, and create a template named "xinneng. (Note: All "$ {}" is a template variable. For example, $ {line_selection} indicates the code segment selected by the current cursor, and $ {cursor} indicates the position of the cursor after the code generation ends, you can also refer to the help documentation provided by eclipse for many parameters .)


After that, select the code block to be tested and press the shortcut key Alt + Shift + Z. You can see that xinneng is added to the menu and click xinneng (), eclipse automatically adds the test code we just entered to the upper and lower of the selected code block. Is it cool?
Okay. Through the above brief introduction, you should have a general understanding of the "Code Template" function of eclipse. If you want to further study this function, you can go to the relevant help documentation and materials.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.