3.5. Creating a Java Method
Problem
You want to create a Java method to run your code.
Solution
Enter the method’s code yourself in the JDT editor, or let code assist help out.
Discussion
Java 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 example
public 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 method
Select 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 template
Fill 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access