There is an even quicker method, however. Just try the same thing again, but now enter only the letters sy and press Ctrl+Spacebar. From the pop-up list, select the entry sysout. (If you continue typing, the pop-up list will get smaller and smaller, because it displays only entries that match the entered string.) If you select the entry sysout with a single click, another pop-up window appears showing a code proposal for the keyword sysout. You can accept this proposal with a double click or press the Esc key to close both windows. The code proposal shown is based on a code template that is associated with the keyword sysout. These templates are defined under Preferences > Java > Editor > Templates, where you can also create your own templates. This is done similarly to defining entries for code generation (see the Templates section in Chapter 1). It is worth browsing through all these templates, because they can save you substantial typing. While many templates are named to resemble Java constructs (if, while, catch, etc.), other templates bear the names of design patterns. Take, for example, the lazy template. This template generates the following code: if (name == null) { name = new type(arguments); } return name; That is a typical pattern for the lazy assignment of a variable. What you have to do with this pattern is just replace the first occurrence of the string name with the name of your own variable, for example, with myHashMap. This automatically replaces all occurrences of name with myHashMap throughout the pattern! In addition to these Java code templates, there are predefined Javadoc templates. For example, if you enter the character @ within a Javadoc comment, a pop-up window appears showing the available Javadoc keywords. Of course, you can define your own templates. In Chapter 1 you have already modified the typecomment template. Here now is an example for a completely homegrown template. The template generates an if instruction that executes only when the equals() method in the condition is successful. In addition, it make sures that you don t get a null pointer exception. Template equals: if (${name} != null && ${name}.equals(${cursor})) { } This template contains the user-defined variable ${name}. When you apply this template, this variable will be replaced with a real field name (just as in the lazy template). In addition, the template contains the system variable ${cursor}. This variable marks the position of the cursor. When applying the template, just replace the first occurrence of name with the real field name. Then press the Tab key to jump to the predefined cursor position. There you can enter the argument for the equals() method. 29 Effective Programming with Eclipse
Hosting services offered by our company comes with free domain name if you pay at yearly basis. Find out more at New Orleans Web Hosting services.