Wednesday, December 10, 2008

Class File

Adding a Class File

In this section, I walk you through the process of adding a second class file to the HelloApp application to demonstrate some of Eclipse's most useful features for speeding up Java program development by generating code for commonly used class features.


Warning

Unless you've already read Book III, you may get bewildered early on by the code that's presented in this procedure. Don't worry; this code will make complete sense once you read about creating your own classes.

So follow these steps to add a second class to the HelloApp application:

  1. Right-click the JavaAIO branch of the HelloApp project in the Package Explorer and choose New Image from book Class.

    The New Java Class dialog box opens (refer to Figure 4-6).

  2. Set the options for the new class.

    For this class, set the options as follows:

    • Leave the Package text field to JavaAIO.

    • Set the Name text field to HelloSayer.

    • Uncheck the Public Static Void main(String[] args) check box.

  3. Click Finish.

    A new class file named HelloSayer is created and Eclipse opens it in a Java editor.

  4. Add declarations for two public fields named greeting and addressee.

    Add these lines immediately after the line that declares the HelloSayer class. Then, other than the comments and the package statement that appears at the beginning of the class, the class looks like this:

    public class HelloSayer {

    private String greeting;
    private String addressee;

    }
  5. Use a wizard to add a constructor to the class.

    To do that, choose Source Image from book Generate Constructor Using Fields. The Generate Constructor Using Fields dialog box appears, shown in Figure 4-9.

    Image from book
    Figure 4-9: Eclipse can create constructors for you.

    Check both greeting and addressee in the list of fields to initialize, select First Method in the Insertion Point drop-down list, and check the Omit Call to Default Constructor Super() check box. Then click OK. The following code is inserted into the class:

    public HelloSayer(String greeting, String addressee) {
    this.greeting = greeting;
    this.addressee = addressee;
    }
  6. Add the code for a method named sayHello.

    Add the following code after the constructor created in Step 5, immediately before the closing brace (}) in the last line of the program:

    public void sayHello()
    {
    System.out.println(greeting + ", " + addressee
    + "!");
    }

  7. Click the HelloApp.java tab at the top of the Java editor pane.

    The HelloApp.java file comes to the front so you can edit it.

  8. Edit the main method so that it uses the new HelloSayer class.

    Delete the code that was in the main method and replace it with this code:

    public static void main(String[] args) {
    HelloSayer h =
    new HelloSayer("Hello", "World!");
    h.sayHello();
    }

The entire HelloApp.java class now looks like Listing 4-2. Eclipse generated all the code except the two lines within the main method.

The entire HelloSayer class file is shown in Listing 4-1.

Listing 4-1: The HelloSayer Class
Image from book
package JavaAIO;

public class HelloSayer {

private String greeting;
private String addressee;

public HelloSayer(String greeting, String addressee) {
this.greeting = greeting;
this.addressee = addressee;
}

public void sayHello()
{
System.out.println(greeting + ", " + addressee
+ "!");
}

}

Image from book

Listing 4-2: The HelloApp Class
Image from book
package JavaAIO;

public class HelloApp {

/**
* @param args
*/
public static void main(String[] args) {
HelloSayer h =
new HelloSayer("Hello", "World");
h.sayHello();
}

}
Image from book

No comments:

Post a Comment