Java Insert Code block

Source: Internet
Author: User

Tag: The Import South Window Pac cal SIG verifies a

    • Paste 1:

When the code is written to a certain extent, you will find that a lot of code has been repeatedly knocked over N times, and even without exaggeration to say: Closed eyes can be knocked out. A lot of knocking on these repetitive code, in addition to exercise hitting the keyboard speed, basically no other benefits, but the long-term will waste a lot of time, reduce the efficiency of software development. For example: In order to output logger information during program execution, you need to add such a piece of code to each class:

[Java]View PlainCopy
    1. import org.slf4j.logger;  
    2. IMPORT ORG.SLF4J.LOGGERFACTORY;  
    3.   
    4. public CLASS XXXCLASS {  
    5.     /** 
    6.      * LOGGING MECHANISM. 
    7.      */  
    8.     private static logger logger =  Loggerfactory.getlogger (xxxclass. Class);   
    9.       
    10. }   

The only difference in this code between different class files is the name of class: Xxxclass. The average person may strike one letter at a time; a lazy person will CTRL + C and CTRL + V, and then change the Xxxclass, and the wise man can do it with a few letters plus a shortcut key. This article takes the above logger as an example to introduce a more convenient and quick way for smart people to use Java Code template.

1. Template settings

On the Eclipse toolbar, choose Windows, Preferences, Java, Editor, and Templates to enter the following interface:

The list is the code Template that comes with Eclipse, and the last column is "on" that is valid, and no representation is invalid. If you want to bring your own code template into effect, you can select the code template you want, click "Edit ...", and in the Open Edit Template dialog box, select the "Automatically insert" check box and click "OK" Save the settings on the line. The use of these valid code template methods is the same as the use of the logger shown later in this paper.

Click "New ..." to create a new template with the following detailed settings:

In the place where you want to declare logger, enter the template name logger, and then press CTRL + Space shortcut to display the template logger as shown in the Prompt box:

Select the template logger you want to add, the code in the template is automatically added, including the import's packages, comments, and logger declarations.

2. Ctrl + Space Failure

In some cases, after you press CTRL + Space shortcut, there is no response, and the prompt box does not appear. There are two possible scenarios:

    • Case 1: Content assist shortcut key not set

On the Eclipse toolbar, choose Window---Preferences, general and keys, and enter the following shortcut key setting interface:

Enter "content assist" in the edit box, will automatically search for "content assist" command, after selecting it, enter "Ctrl+space" in the "Binding" edit box below, or press Ctrl + Space shortcut directly, then click " OK ".

    • Case 2:ctrl + Space accelerator conflict

If CTRL + Space is already set to the content assist shortcut, but it still doesn't work, the possible reason is that eclipse's Ctrl + Space shortcut conflicts with the shortcut keys in the IME switch in the Windows system. Change the shortcut key for the IME switch to another, as shown in:

3. The Ctrl + Space prompt box does not have a template

If CTRL + space is pressed, the template logger is not displayed in the Prompt box, as shown in:

This is because the content Assist does not have a template hint selected (templates proposals). On the Eclipse toolbar, select Content Assist--------Java--------Preferences, which can be set up as follows:

Select the check box in front of the "Template proposals" in the two box. If the "Template proposals" in the box below is not the first one, you can select it and then click the "Up" button until it is in the first position so that the first one in each prompt box is the code Template you want.

    • Paste 2:

One day when debugging the code, although it is Android project or the habit of input syso, and then alt+/a bit. Next to the colleague asked, what is the tag of this log printout. Then asked why Syso can intelligently associate this. The first question is a good answer, but I really can't answer the second question. The first time in the university to see a senior use this skill, then silently remember, and did not think much of what is going on. Notice these two days, originally is similar Java code template of an east. In a wonder why don't you define your own Android's Lenovo Word ...
Step one: Open preference, search editor, select Java Editor, this time to find Sysout can see its definition


Step Two: This time we customize the log printing of Android, choose New, input string, notice that the middle segment similar to the regular expression is imitated sysout, want to be the cursor moved to here meaning


Step three: Verify that, enter Loe, press and hold alt+/, automatically generate the code shown below, and the cursor is still there


Soon Jie has wood, no longer need to debug the output of a sentence log for half a day, but also with the function of the call stack Oh.

    • My Practice:

The following code,

${:import(Java.util.Scanner)} can import packages at the beginning
"${word_selection}${}" to position the cursor.
${:import(java.util.Scanner)}scanner Scan=new Scanner (system.in); // Generate input Flu System.out.print ("${word_selection}${}"); // Input Reminder=scan.nextint (); // Assignmentscan.close ();

You can also import missing packages by pressing Ctrl+shift+o directly at the beginning without starting a line.

Java Insert Code block

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.