Introduction to Java Package
A Java package is a mechanism for organizing a group of related files in the same directory and having each class file in a package directive with that directory name at the top of the file. Programmers also use package convention to organize classes that belong to the same files or providing similar functionality.
Java source files can include a package statement at the top left of the file to designate the package for the classes in which the source file defines. If we are not including any package in our java source file then the source file automatically goes to the default package.
Features of a Java package
The package to which the source file belongs is specified with the keyword package at the top left of the source file.
The source file HelloWorld.java will be saved in the package named mypackage.
Access protection in packages
No modifier (default): In case of no modifier, the classes and members specified in the same package are accessible to all the classes inside the same package.
public: The classes, methods and member variables under this specifier can be accessed from anywhere.
protected: The classes, methods and member variables under this modifier are accessible by all subclasses, and accessible by code in same package.
private: The methods and member variables are accessible only inside the class.
Access to fields in Java at a Glance:
|The class itself||Yes||Yes||Yes||Yes|
|A subclass in same package||Yes||Yes||Yes||No|
|Non sub-class in the same package||Yes||Yes||Yes||No|
|A subclass in other package||Yes||Yes||No||No|
|Non subclass in other package||Yes||No||No||No|
Naming convention of packages
A hierarchical naming pattern is used for java packages, with levels separated by dots in the hierarchy. The packages that comes lower in the naming hierarchy are called "subpackage" of the corresponding package higher in the hierarchy. Java uses the package naming conventions in order to avoid the possibility of source file having the same name. The naming convention defines how to create a unique package name, so that packages that are widely used with unique namespaces. This allows packages to be easily managed.
In general, we starts a package name begins with the order from top to bottom level. Package names should be in lowercase characters whenever possible.
Packages in Core Java
|java.lang||This package includes basic language functionality and fundamental types|
|java.io||This package deals various input/output file operations.|
|java.util||This package handles collection of data structure classes.|
|java.math||This package includes various mathematical operations.|
|java.sql||This package covers Java Database Connectivity (JDBC) to access databases.|
|java.nio|| This package handles New I/O framework for Java
|java.net||It is used for networking operations, sockets, DNS lookups, ...|
|java.security|| It is used for key generation, encryption and decryption
|java.awt|| It is used for basic hierarchy of packages for native GUI
|java.swing||It is used for hierarchy of packages for platform-independent rich GUI components.|
Recommend the tutorial