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.
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:
gui
(so that it looks like com.creatingpackages.gui).A new empty package will be shown right under com.creatingpackages.
And finally, a package for our unit test source code:
A new empty test package named com.creatingpackages.tests.gui is shown below Test Packages.
The final setup should look like this:
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.
It is also possible to create packages in the Files tab and with a new Class creation wizard.
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.
3.143.22.58