Creating a package

Everything is better when organized. With this in mind, we will check how to create packages using the IDE.

Besides being more organized, it is bad coding practice to leave all classes in the same package or in the root package.

How to do it...

First we will need to create a new project, so please refer to our recipe Creating a Java Project using Wizard in the beginning of this chapter, for project creation. When naming the project, enter CreatingPackages. When the Projects tab shows the CreatingPackages project, expand the CreatingPackages node, if not yet expanded.

Now we will create a package for our source code:

  1. Expand the Source Packages node (if not yet expanded).
  2. Right-click on com.creatingpackages.
  3. Select New and then click on Java Package....
  4. In the Package Name text field, append gui (so that it looks like com.creatingpackages.gui).
  5. Leave Location set as Source Packages.
  6. Click Finish.

A new empty package will be shown right under com.creatingpackages.

And finally, a package for our unit test source code:

  1. Expand the Test Packages node (if not yet expanded).
  2. Right-click on Test Packages.
  3. Select New and then click on Java Package....
  4. In the Package Name text field, type com.creatingpackages.tests.gui.
  5. Leave Location set as Test Packages.
  6. Click Finish.

A new empty test package named com.creatingpackages.tests.gui is shown below Test Packages.

The final setup should look like this:

How to do it...

How it works...

Having understood that, we will see both ways of creating a package: one, by clicking on the desired folder destination, where the package will reside and the other, by clicking where the root node of the package will be.

By right-clicking on com.creatingpackages, the IDE will understand that we wish to create a package under the current one and will then automatically append the full path of the packages in Package Name. The user then needs to only type the rest of the path. This saves a lot of time when the project grows and nested packages start to spread.

The second option is to right-click directly on the desired node in the Packages tab. In our example we are creating a package under Test Packages. Right-clicking on the Test Packages node will trigger a clean Package Name and it is up to the developer to decide what the full path is going to be.

There's more...

It is also possible to create packages in the Files tab and with a new Class creation wizard.

The Files tab

By navigating to the Files tab, it is possible to see how the folder structure is organised, similarly to the Projects tab. It is also possible to create a Package using this view by following the same steps described previously.

The Files tab differs from the Project view in the sense that the files are presented as they exist in the filesystem. The Project view on the other hand, presents the files as they are organized from the project perspective.

Automatic creation of packages

It is also possible to create packages when a new class is created by the IDE.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.22.58