< Day Day Up > |
Recipe 1.9 Completing Code Automatically1.9.1 ProblemWhile entering code, you forgot the name of either a method you wanted to call or some of a method's parameters. 1.9.2 SolutionUse Eclipse's code assist (also called content assist) to help out. When you enter the name of an object or class in the JDT code editor followed by a period (.) and then pause, code assist displays the members of that object or class, and you can select the one you want. You also can bring up code assist at any time (e.g., when you've positioned the cursor inside a method's parentheses, and you want to see what arguments that method takes) by pressing Ctrl-Space or by selecting Edit Content Assist. 1.9.3 DiscussionCode (or content) assist is one of the good things about using a full Java IDE. It's an invaluable tool that accelerates development, and it's a handy resource that you'll probably find yourself relying on in time. In the code example we've been developing over the previous few recipes, enter the following code to display some text: public class FirstApp
{
public static void main(String[] args)
{
System.out.println("Stay cool.");
}
} To work with code assist, enter System. in the main method of the FirstApp project, then pause. Code assist displays the classes and methods in the System namespace, as shown in Figure 1-11. Figure 1-11. Using code assistDouble-click out in the code assist list so that code assist inserts that member into your code, insert a period so that the phrase now reads System.out., and pause again. Code assist now displays the methods of the out class. Double-click the code assist suggestion println(String arg0), and code assist inserts the following code into the main method: public class FirstApp
{
public static void main(String[] args)
{
System.out.println( )
}
} Edit this to add the text Stay cool.. Note that code assist adds the closing quotation mark automatically as you type: public class FirstApp
{
public static void main(String[] args)
{
System.out.println("Stay cool.")
}
} As soon as you enter this code, Eclipse displays it with a wavy red underline, shown in Figure 1-12, to indicate that a syntax problem exists. Rest the mouse cursor over the new code, and a tool tip appears, also shown in Figure 1-12, indicating that a semicolon is missing. Note also that a red box (displayed in stunning black and white in the figure) appears in the overview bar to the right of the code. Clicking that box jumps to the error, which is handy if you've got a lot of errors and a long code file.
Figure 1-12. A syntax error messageAdd that semicolon now to the end of the line to give you the complete code and to make the wavy red line disappear.
Finally, save the file by clicking the disk icon in the toolbar or by selecting File Save. An unsaved file appears with an asterisk before its name in its editor tab (as shown in Figure 1-12), but the asterisk disappears when the file is saved. If you don't save a code file before trying to compile and run that code, Eclipse will prompt you to do so. We'll run this code in the next recipe. To sum up, code assist is a great tool for code completion, and it will start automatically when you insert a period (.) in the JDT editor after the name of an object or class. You also can make code assist appear at any time while you're typing code; just press Ctrl-Space or select Edit Content Assist.
1.9.4 See AlsoRecipe 1.10 on running your code; Chapter 1 of Eclipse (O'Reilly). |
< Day Day Up > |