Java package,Java Packages

This page discusses - Java package,Java Packages

Java package,Java Packages

Java Package


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 protected members of the classes can be accessed by each other in the same package.
  • It resolves the naming collision for the types it contains.
  • A package may have the following types.
    • Interfaces
    • Classes
    • Enumerated types
    • Annotations

Using packages

The package to which the source file belongs is specified with the keyword package at the top left of the source file. 

package mypackage;
class HelloWorld {
  public static void main (String args[]) {
  System.out.println("Hello World!");

The source file 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:

Access By public protected default private
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

Package Description
java.lang This package includes basic language functionality and fundamental types 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 It is used for networking operations, sockets, DNS lookups, ... It is used for key generation, encryption and decryption.
java.awt It is used for basic hierarchy of packages for native GUI components.
java.swing It is used for hierarchy of packages for platform-independent rich GUI components.