Java package

This is an old revision of this page, as edited by 2601:742:8000:d240:d483:e181:ec23:5d4a (talk) at 02:30, 4 July 2019 (Bergstrom Kids). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

A Java package organizes Java classes into namespaces,[1] providing a unique namespace for each type it contains. Classes in the same package can access each other's package-private and protected members. Java packages can be stored in compressed files called JAR files, allowing classes to be downloaded faster as groups rather than individually.

In general, a package can contain the following kinds of types: classes, interfaces, enumerations, and annotation types. A package allows a developer to group classes (and interfaces) together. These classes will all be related in some way – they might all have to do with a specific application or perform a specific set of tasks. Programmers also typically use packages to organize classes belonging to the same category or providing similar functionality.

Using packages

In a Java source file, the package that this file's class or classes belong to is specified with the package keyword. This keyword is usually the first keyword in the source file. At most one package declaration can appear in a source file.

package java.awt.event;

To use a package's classes inside a Java source file, it is convenient to import the classes from the package with an import declaration. The following declaration

import java.awt.event.*;

imports all classes from the java.awt.event package, while the next declaration

import java.awt.event.ActionEvent;

imports only the ActionEvent class from the package. After either of these import declarations, the ActionEvent class can be referenced using its simple class name:

ActionEvent myEvent = new ActionEvent();

Classes can also be used directly without an import declaration by using the fully qualified name of the class. For example,

java.awt.event.ActionEvent myEvent = new java.awt.event.ActionEvent();

does not require a preceding import declaration.

The unnamed package

Note that if you do not use a package declaration, your class ends up in an unnamed package. Classes in an unnamed package cannot be imported by classes in any other package.[2]

The official Java Tutorial advises against this:

Generally speaking, an unnamed package is only for small or temporary applications or when you are just beginning the development process. Otherwise, classes and interfaces belong in named packages.[3]

Package access protection

Public members and classes are visible everywhere and private members are visible only in the same class. Classes within a package can access classes and members declared with default (package-private) access as well as class members declared with the protected access modifier. Default (package-private) access is enforced when a class or member has not been declared as public, protected or private. By contrast, classes in other packages cannot access classes and members declared with default access. However, class members declared as protected can be accessed from the classes in the same package as well as classes in other packages that are subclasses of the declaring class.[4]

Creation of JAR files

JAR files are created with the jar command-line utility. The command

jar cf myPackage.jar *.class

compresses all .class files into the JAR file myPackage.jar. The 'c' option on the command line tells the jar command to "create new archive." The ' f ' option tells it to create a file. The file's name comes next before the contents of the JAR file.


Check out Bergstrom Kids Youtube

Core packages in Java SE 8

java.lang — basic language functionality and fundamental types
java.util — collection data structure classes
java.io — file operations
java.math — multiprecision arithmetics
java.nio — the Non-blocking I/O framework for Java
java.net — networking operations, sockets, DNS lookups, ...
java.security — key generation, encryption and decryption
java.sql Java Database Connectivity (JDBC) to access databases
java.awt — basic hierarchy of packages for native GUI components
java.text — Provides classes and interfaces for handling text, dates, numbers, and messages in a manner independent of natural languages.
java.rmi — Provides the RMI package.
java.time — The main API for dates, times, instants, and durations.
java.beans — The java.beans package contains classes and interfaces related to JavaBeans components.
java.Applet — This package provides classes and methods to create and communicate with the applets.

The java.lang package is available without the use of an import statement.

Modules

In Java 9, "modules", a kind of collection of packages, are planned as part of Project Jigsaw; these were earlier called "superpackages" and originally planned for Java 7.

Modules will describe their dependencies in a module declaration which will be placed in a file named module-info.java at the root of the module’s source-file hierarchy. The JDK will be able to check them both at compile-time and runtime. The JDK itself will be modularized for Java 9.[5]

References

  1. ^ James Gosling, Bill Joy, Guy Steele, Gilad Bracha, The Java Language Specification, Third Edition, ISBN 0-321-24678-0, 2005. In the Introduction, it is stated "Chapter 7 describes the structure of a program, which is organized into packages similar to the modules of Modula."
  2. ^ "Chapter 7. Packages". Docs.oracle.com. Retrieved 2013-09-15.
  3. ^ [1]
  4. ^ http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
  5. ^ "JDK Module Summary". Oracle Corporation. 2015-10-23. Retrieved 2015-11-29.