Purpose of a package, in Java

What is a package?

A Package is set of Classes, interfaces or sub packages.

Advantage of package

Below are advantages of packages
1. To avoid name collisions between classes or interfaces. Using different packages it is possible to have Classes or interfaces with same name.
2. Improves Modularity of Applications. As application development, is done across various Teams spread across various organizations, it is advantage to have each software module or layer in a package, and each sub module may further have sub package.
3. Another advantage of packages is, packages provides one more level of accessibility. Class members or classes with default or no access specifier, are accessible only within, current package.

All public classes and interfaces can be accessed by other packages also.
All classes and interfaces with default or none access specifier, can be accessed only within current package.

Below is diagram showing two packages com.java652 and com.p1. The package com.java652 has two classes WeatherDetails and CityDetails. The package com.p1 has one class StateDetails and an interface. objects of classes in different packages interact with each other, to perform required functionality.

What is Java Package
Java Package

Keywords used for packages





Below are keywords used in packages
1. package
package keyword is used to specify to which package, current java source file belongs to. There can be only package statement in a file. package statement need to be first statement in the file.
2. import
import keyword is used to specify that classes or interfaces of other packages need to be used in current java file. There can be multiple import statements, in a single file.

Also note that a separate folder is created with package name as folder name. And all class and interface files belonging to this package, are in this folder.

Download Package example(Eclipse Project)

For each sub package, one sub folder is created, under the main folder.

How to create package

how to create package, manually

When you develop java project using command line, and simple text editor, you need to manually create the package.
That means you need to manually create folders, and sub folders, for packages and sub packages, accordingly. Place java source file in corresponding folders or sub folders.

How to create package on Eclipse

As shown in below picture,
Right click on project, for which you want to create a package.
Now select New, then select package, then fill the package name like com.java652
To create class in this package, right click on package, select New, then select Class, then fill the class name.

how to create package in eclipse
how to create package in eclipse

How to create package on Netbeans

As shown in below picture,
Right click on project, for which you want to create a package.
Now select New, then select Java Package, then fill package name
To create class in this package, right click on package, select New, then select class.

how to create package in netbeans
how to create package in netbeans

you may also like to read:
How many Access specifiers in Java?
Can a abstract class have static method