Java Development on Eclipse, Part 1
by Steve HolznerAuthor's note: In part one of a two-part series of excerpts from Eclipse's Chapter 2, we'll get down to the business of developing Java using Eclipse. We're going to take a look at using Eclipse for Java development as well as project management, going from the basics to the fairly serious. Nearly everything in this chapter is essential knowledge for the Java developer using Eclipse, so let's jump in.
Developing Java Code
If there's anything that takes more time than it seems to be worth in Java, it's creating code from scratch. While the logic inside a method, interface, or class is unique, the modifiers of a method, the imports for a class, and the syntax involved with new packages is the same over and over again. This often results in a lot of repetitive typing, wasted time, and in many cases, annoying little typo-related bugs. Eclipse can help with all this and more.
Creating New Methods
Eclipse - through code assist - makes it easy to create new methods. As an
example, we're going to create and call a new method named printer,
which displays the message "No worries.", as you can see in Example 2-1.
Example 2-1. The Ch02_01.java example
public class Ch02_01
{
public static void main(String[] args)
{
printer( );
}
private static void printer( )
{
System.out.println("No worries.");
}
}
How do you create new methods? Start Eclipse now and create a new project
named Ch02_01. Then create a new Java class named
Ch02_01, making it part of the org.eclipsebook.ch02
package. Leave the checkbox for the creation of a stub for the main
method checked when you create this new class. This gives you the code:
public class Ch02_01 {
public static void main(String[] args) {
}
}
You could simply type in the printer method, of course, but
Eclipse can also be of assistance here. Move the cursor below the body of the
main method and type private to make this new method a
private method, and then type Ctrl+Space to open code assist, as you see in
Figure 2-1.

Figure 2-1. Creating a private method
Code assist lets you select the type of private method you want to
create private, private static, and so on.
Here, select the private static method, creating the
new method template you see in Figure 2-2. The placeholder
return_type is highlighted, ready for you to enter in a type; use
void. Next, replace the name placeholder with the name
printer and delete the arguments placeholder.

Figure 2-2. Setting the new method's return type
This creates the new method in outline; all that's left is to add the code that will display the message (as before, you can take advantage of code assist to pause after you type each dot for suggestions):
public static void main(String[] args) {
}
private static void printer( )
{
System.out.println("No worries.");
}
Then just add the call to printer from main:
public static void main(String[] args) {
printer( );
}
private static void printer( )
{
System.out.println("No worries.");
}
That's all you need; now you can run the example with the Run As→ Java Application menu item. You should see "No worries." appear in the Console view just as before. But this time, we're using a new custom method.
Creating New Classes
We've created a new method and given it a name, but what if you want that new
method to be in a different class? For example, say that your main
method is in a class named Ch02_02, but the printer
method is in a class named Ch02_02Helper:
Ch02_02
|
+--------main
Ch02_02Helper
|
+--------printer
In this case, you could create a new object of the Ch02_02Helper
class in the main method, and then you could call that object's
printer method, as you see in Example 2-2.
Example 2-2. The Ch02_02.java example
public class Ch02_02 {
public static void main(String[] args) {
Ch02_02Helper helper = new Ch02_02Helper( );
helper.printer( );
}
The Ch02_02Helper class, with the printer method in
it, appears in Example 2-3.
Example 2-3. The Ch02_02Helper.java example
public class Ch02_02Helper {
public void printer( ) {
System.out.println("No worries.");
}
}
To implement this example with its two classes in Eclipse, create a new
project named Ch02_02 and add the new class
Ch02_02Helper to the project. Make this class public
and put it into the org.eclipsebook.ch02 package (and make sure
that you don't create a main method stub in this class). Next, add
the printer method to this class, as in Example 2-2.
Then save the file. This is an important step because if you don't
save the file, the printer method won't be available to the rest of
your project's code. This is essential to know whenever you want to work with
items from file A in file B, you have to save file A first because Eclipse
compiles from the files, not what's in its editors.
