< Day Day Up > |
Recipe 3.5 Creating a Java Method3.5.1 ProblemYou want to create a Java method to run your code. 3.5.2 SolutionEnter the method's code yourself in the JDT editor, or let code assist help out. 3.5.3 DiscussionJava code must be in a method to be run. Eclipse, with code assist, can help out. Say you want to create the code you see in Example 3-1, which includes a new method named display. Example 3-1. The DisplayApp.java examplepublic class DisplayApp { public static void main(String[] args) { display( ); } private static void display( ) { System.out.println("No problem."); } } To see what code assist can do for you, create the DisplayApp project, and let Eclipse generate a main method for you: public class DisplayApp { public static void main(String[] args) { } } Instead of typing in the display method by hand, use code assist. Move the cursor beneath the main method, and enter the word private to make display a private method. Then, press Ctrl-Space to open code assist, as shown in Figure 3-8. Figure 3-8. Creating a new methodSelect the private static method item, and code assist will create the method template for you, as shown in Figure 3-9. Figure 3-9. A new method templateFill in the return_type, name, and arguments items to give you the code in Example 3-1; save the file; and run the project by selecting Run Run As Java Application. You should see "No problem." in the Console view.
|
< Day Day Up > |