Chapter 2. Java Development

Create Java project, packages, classes, methods

Java project

Java projects contain the resources needed for Java applications, including Java files and class files. Java projects are used to create Java packages. When you create a new Java project, the environment is set up for Java development. The Java project contains information about the type hierarchy and Java elements. This information is kept current as changes are made.

A Java builder is associated with the project so the Java source is incrementally compiled, automatically, as it is updated. Web, EJB, and application client projects can also contain the resources needed for Java applications. The Java builder will incrementally compile the resources within these projects as the resources are updated.

Java packages can be exported as JAR files and included in the build path for other project types, or in the class path for application servers.

Each Java project maintains additional information about the type hierarchy and the references and declarations of Java elements. This information is constantly updated as the user changes the Java source code; it is not dependent on the builder.

Java project organization

You can organize Java projects in two different ways:

  1. Using the project as the source container. In this organization, all Java packages are created directly inside the project. This is the recommended organization for simple projects. The generated .class files are stored along with the .java source files.

  2. Using source folders as the source container. In this organization, packages are created not directly inside the project, but in source folders. You create source folders as children of the project and create your packages inside these source folders. This is the recommended organization for more complex projects. It allows you to subdivide packages into groups.

Depending on the file organization that you choose when creating the project, the class files are either stored along with the Java files in the project folder or, if you are using source folders, the class files are stored in a separate output folder. The builder will also copy non-Java resources (for example, GIF files) into the output folder. If you have non-Java resources that should not be copied into the output folder, for example a readme file, you can create an ordinary folder and store the resources there.

Creating a Java project

  1. Open the New Project wizard by selecting the menu item File > New > Project ....

  2. On the left pane of the first wizard page, select Java, and on the right pane, select Java Project. Then click Next.

  3. In the Project name field, type new project name, then click Finish. A new Java perspective opens within the workbench with the new Java project in the Package Explorer.

    Java Project name

    If you click Next, you can change new Java project properties:

    Java Project Properties

Creating a new Java package

To create new Java packages in the Package Explorer:

  1. Optionally, select the project or source folder where you want the package to reside.

  2. Click the Create a Java Package button in the workbench toolbar. The New Java Package wizard opens.

    Create a Java Package button

    You can do the same by selecting the menu item File > New > Package.

  3. Edit the Source Folder field to indicate in which container you want the new package to reside. You can either type a path or click Browse to find the container. If a folder was selected when you chose to create the new package, that folder appears in the Source Folder field as the container for the new package.

  4. In the Name field, type a name for the new package.

    Type a name for the new package

  5. Click Finish when you are done.

Note: the default (unnamed) package always exists and does not need to be created.

Creating a new Java class

Use the New Java Class wizard to create a new Java class. There are a number of ways to open this wizard:

  1. Select the container where you want the new class to reside.

  2. Click the Create a Java Class button in the workbench toolbar.

    Create a Java Class button

or

  1. Select the container where you want the new class to reside.

  2. From the container's pop-up menu, select New > Class.

    New > Class

or

  1. Select the container where you want the new class to reside.

  2. From the drop-down menu on the Open The New Wizard button in the workbench toolbar, select Class.

    Open The New Wizard > Class

or

Click the Open The New Wizard button. In the left pane, select Java, and in the right pane, select Class.

Java > Class

or

Select the container where you want the new class to reside. Then, select from the menu bar File > New > Class.

File > New > Class

This wizard helps you to create a new Java class in a Java project:

New Java Class

Create new methods

  1. Confirm that the alphabetical Sort button in the toolbar of the Outline view is toggled off.

  2. Start adding a method by typing the following at the end of the Java file (but before the closing brackets of the type) in the Java editor:

    public String toString() {								
    								
    As soon as you type the method name in the editor area, the new method appears at the bottom of the Outline view.

  3. Click the Save button. The compilation unit is compiled automatically and errors appear in the Package Explorer view, in the Tasks view and on the vertical ruler. In the Package Explorer view, the errors are propagated up to the project of the compilation unit containing the error.

  4. Complete the new method by typing the following:

    	return str;	
    }								
    								

  5. Save the file. Notice that the error indicators disappear.

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCBCD 1.3 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free Mock Exam Engine