Leveraging eclipse's code template capabilities

Source: Internet
Author: User

Eclipse is a very powerful IDE, and as a Java programmer, it deals with it almost every day, but does it use powerful functions?

Let's look at the Code templates feature in eclipse today. What is a code template? For example, a very common application, how to create a main function in a class?

That's not easy? The voice did not fall, I saw you on the keyboard on the fast percussion, rinsed will be done.

OK, typing is quick to say. But isn't it supposed to be the case that for this very fixed code, it's necessary to strike a letter with a letter?

As an IDE, one of the design philosophies is to help the user do the work, as in the case above, it is time for the Eclipse Code template to work.

We simply type main in the class body and then use Eclipse's code hint shortcut (default = alt+/), and after carriage return, we can see that eclipse automatically completes the full definition of the main function for us, is it very convenient?

This is a simple example, but eclipse also provides many code templates by default. Open windows->preferences->java->editor->templates to see a list of all defined code templates.

To give a more common example, it is often necessary to output some data from the console in the code

System.out.pringln ("Hello, World");

Before if you have been used to a letter of a letter to knock, then you can now try typing sysout, and then press alt+/, hehe, the entire statement is automatically completed, and the cursor is also very thoughtful to stop in parentheses, so that you continue to input, is not very powerful?

Sometimes there are other kinds of problems in programming, such as I want to add some code to the Try/catch block for exception detection. With the help of the code template, you do not have to manually type this part of the code, and do not have to control the code indentation, you only need to select the code block to be processed, and then press Alt+shift+z, enter, Eclipse will automatically pack the Try/catch statement for the selected code block.

Although Eclipse has provided us with many useful templates by default, it is certainly not enough, so we need to try to create our own code templates.

As a practical example, we will often need to test the execution time of a piece of code in actual development, in general we will use the following methods.

Record the time stamp before and after the code is executed, and then calculate the difference of two timestamps. This code is very solid, if you manually typed it every time, it is very frustrating, but if you use the "Code template" function to do, it is much more convenient.

Open Windows->preferences->java->editor->templates, click New ..., create a new code template, and enter the code as shown, creating a template named "Xinneng".

(Note: All "${}" are template variables, such as ${line_selection}, which represents the code snippet currently selected by the cursor, ${cursor} indicates where the cursor is at the end of the code generation, and there are many parameters you can refer to the help documentation provided by Eclipse. )

When you are finished, select the code block you want to test, use the shortcut key alt+shift+z, you can see a menu of more than one Xinneng, click Xinneng (), Eclipse automatically in the selected code block up and down add the test code we just entered, cool it?

Well, with the simple introduction above, you should have a general understanding of the "Code template" feature of Eclipse, if you want to do a more in-depth study of this function, you can consult the relevant help documents and materials.

Leveraging eclipse's code template capabilities

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.