Chapter 2. Java Development

Create Java projects, packages, classes, and methods

Java projects

A Java project contains source code and related files for building a Java program. It has an associated Java builder that can incrementally compile Java source files as they are changed.

A Java project also maintains a model of its contents. This model includes information about the type hierarchy, references and declarations of Java elements. This information is constantly updated as the user changes the Java source code. The updating of the internal Java project model is independent of the Java builder; in particular, when performing code modifications, if autobuild is turned off, the model will still reflect the present project contents.

You can organize Java projects in two different ways:

  • Using the project as the source container. This is the recommended organization for simple projects.

  • Using source folders inside the project as the source container. This is the recommended organization for more complex projects. It allows you to subdivide packages into groups.

Creating a Java project as its own source container

For simple projects, the project itself acts as the source container.

  1. From the main workbench window, click File > New > Project. The New Project wizard opens.

  2. Select Java from the left pane and Java Project from the right pane, then click Next. The New Java Project wizard opens.

  3. In the Project Name field, type a name for your new Java project.

    Creating a Java project

  4. Select a location for your Java project:

    • Create project in workspace: Select this option to save your Java project in this workspace.

    • Create project at external location: Select this option to save your Java project in an external location (another directory or server location). Click Browse to select the location for your project.

    Specify the project layout that you want:

    • Use project folder as root for sources and class files: Select this option if you want to save all your generated files within this Java project folder.

    • Create separate source and output folders: Select this option if you want to save your source in a different location from your application output.

  5. Click Next. The Java Settings Page opens.

    Java Settings Page

  6. On the Source tab, check that the project is the only source folder and the default output folder.

  7. Optionally, on the Projects tab, select the required projects to be on the build path for this project. Use these options only if your project depends on other projects.

  8. Optionally, on the Libraries tab, select JAR files and CLASS folders to add to the build path for this new project and attach source to the JAR files. Use these options only if your project requires additional libraries.

  9. On the Order and Export tab, use the Up and Down buttons to move the selected JAR file or CLASS folders up or down in the build path order for this new project.

  10. Click Finish when you are done.

    Java Project

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 New Java Package button in the workbench toolbar. The New Java Package wizard opens.

    New Java Package button

  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.

    New Java Package Name

  5. Click Finish when you are done.

    New Java Package

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 New Java Class button in the workbench toolbar.

    New Java Class

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 New button in the workbench toolbar, select Class.

or

Click the New button in the workbench toolbar to open the New wizard. In the left pane, select Java, and in the right pane, select Class.

or

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

Creating a top-level class

You can create classes that are not enclosed in other types.

  1. Open the New Class wizard.

    New Class wizard

  2. Edit the Source Folder field as needed to indicate in which folder you want the new class to reside. You can either type a path or click the Browse button to find the folder. If a folder is found for the current selection, that folder appears in the Source Folder field as the container for the new class.

  3. In the Package field, type or click Browse to select the package where you want the new class to reside. Leave this field empty to indicate that you want the new class to be created in the default package.

  4. Leave the Enclosing type box deselected.

  5. In the Name field, type a name for the new class.

  6. Select the public or default access modifier using the Modifiers radio buttons.

    Note: The private and protected options are available only when creating a class enclosed in a type.

  7. Optionally, select the abstract or final modifier for the new class using the appropriate check boxes.

    Note: The static option is available only when creating a class enclosed in a type.

  8. In the Superclass field, type or click Browse to select a superclass for the new class.

  9. Click the Add button to add interfaces for the new class to implement.

  10. If you want to create some method stubs in the new class:

    • Select the public static void main(String[] args) box if you want the wizard to add the main method to the new class, thus making it a starting point of an application.

    • Select the Constructors from superclass check box if you want the wizard to create, in the new class, a set of constructors, one for each of the constructors declared in the superclass. Each of them will have the same number of parameters (of the same types) as the respective constructor from the superclass.

    • Select the Inherited abstract methods check box if you want the wizard to generate method stubs for each of the abstract methods that the new class will inherit from its superclass and implemented interfaces.

  11. Click Finish when you are done.

    New Class

Creating a nested class

You can create classes that are enclosed in other types (that is, nested classes).

  1. Open the New Java Class wizard.

    Creating a nested class

  2. Edit the Source Folder field to indicate in which folder you want the new class to reside. You can either type a path or click Browse to find the folder. If a folder is found for the current selection, that folder appears in the Source Folder field as the container for the new class.

  3. Select the Enclosing Type check box and type the name of the enclosing type in the Enclosing Type field. You can also click Browse to select the enclosing type for the new class.

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

  5. Select the desired modifiers by using the Modifiers radio buttons and check boxes.

  6. In the Superclass field, type or click Browse to select a superclass for the new class.

  7. Click the Add button to add interfaces for the new class to implement.

  8. If you want to create some method stubs in the new class:

    • Select the public static void main(String[] args) check box if you want the wizard to add the main method to the new class, thus making it a starting point of an application.

    • Select the Constructors from superclass check box if you want the wizard to create, in the new class, a set of constructors, one for each of the constructors declared in the superclass. Each of them will have the same number of parameters (of the same types) as the respective constructor from the superclass.

    • Select the Inherited abstract methods check box if you want the wizard to generate method stubs for each of the abstract methods that the new class will inherit from its superclass and implemented interfaces.

  9. Click Finish when you are done.

    Nested Java Class

NOTE: The new class is created in the same compilation unit as its enclosing type.

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