Bodhak java packages tutorial in this chapter we will cover I detail about How Creating the package?, Benefits of using package, How to compile the package, How to use package members in another program/ import statement, Creating package within a package, About static import.
A Package can be defined as a grouping of related types(classes, interfaces, enumerations, and annotations ) providing access protection and name space management.
java packages tutorial
Packages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc.
Programmers can define their own packages to bundle a group of classes/interfaces, etc. It is a good practice to group related classes implemented by you so that a programmer can easily determine that the classes, interfaces, enumerations, annotations are related.
How Creating the package?
When creating a package, you should choose a name for the package and put a package statement with that name at the top of every source file that contains the classes, interfaces, enumerations, and annotation types that you want to include in the package.
The package statement should be the first line of the source file. There can be only one package statement in each source file, and it applies to all types of the file.
If a package statement is not used then the class, interfaces, enumerations, and annotation types will be put into an unnamed package.
Benefits of using package:
Anyone can easily determine which files are related.
The namespace collision is minimized.
One can allow types in one package to have unrestricted access to one another, still restricting the access for the types outside the package.
Java packages can be stored in compressed files called JAR files, thus allowing the classes to download faster as a group rather than downloading each one at a time.