Java package

Last updated

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.

Contents

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.

packagejava.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

importjava.awt.event.*;

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

importjava.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:

ActionEventmyEvent=newActionEvent();

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

java.awt.event.ActionEventmyEvent=newjava.awt.event.ActionEvent();

does not require a preceding import declaration.

Package-wide Javadoc & annotations

Documentation explaining the package as a whole is written as Javadoc in a file named exactly `package-info.java`. That file is also the place for annotations to be used across all classes of the package. [2]

The unnamed package

If a package declaration is not used, classes are placed in an unnamed package. Classes in an unnamed package cannot be imported by classes in any other package. [3] 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. [4]

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. [5]

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.

Package naming conventions

Packages are usually defined using a hierarchical naming pattern, with some levels in the hierarchy separated by periods (., pronounced "dot"). Although packages lower in the naming hierarchy are often referred to as "subpackages" of the corresponding packages higher in the hierarchy, there is almost no semantic relationship between packages. The Java Language Specification establishes package naming conventions to avoid the possibility of two published packages having the same name. The naming conventions describe how to create unique package names, so that packages that are widely distributed will have unique namespaces. This allows packages to be separately, easily and automatically installed and catalogued.

In general, a package name begins with the top level domain name of the organization and then the organization's domain and then any subdomains, listed in reverse order. The organization can then choose a specific name for its package. Subsequent components of the package name vary according to an organization's own internal naming conventions. [6]

For example, if an organization in Canada called MySoft creates a package to deal with fractions, naming the package ca.mysoft.fractions distinguishes the fractions package from another similar package created by another company. If a German company named MySoft also creates a fractions package, but names it de.mysoft.fractions, then the classes in these two packages are defined in a unique and separate namespace.

Complete conventions for disambiguating package names and rules for naming packages when the Internet domain name cannot be directly used as a package name are described in section 7.7 of the Java Language Specification. [7]

Core packages in Java SE 8

java.langbasic language functionality and fundamental types that is available without the use of an import statement.
java.utilcollection data structure classes
java.iofile operations
java.mathmultiprecision arithmetics
java.niothe Non-blocking I/O framework for Java
java.netnetworking operations, sockets, DNS lookups, ...
java.securitykey generation, encryption and decryption
java.sql Java Database Connectivity (JDBC) to access databases
java.awtbasic hierarchy of packages for native GUI components
java.textProvides classes and interfaces for handling text, dates, numbers, and messages in a manner independent of natural languages.
java.rmiProvides the RMI package.
java.timeThe main API for dates, times, instants, and durations.
java.beansThe java.beans package contains classes and interfaces related to JavaBeans components.
java.appletThis package provides classes and methods to create and communicate with the applets.

Modules

In Java 9 (released on September 21, 2017) support for "modules", a kind of collection of packages, was implemented as a result of the development effort of Project Jigsaw. The "modules" were earlier called "superpackages" and originally planned for Java 7.

Modules describe their dependencies in a declaration placed in a file named module-info.java at the root of the module's source-file hierarchy. Since Java 9, the JDK is able to check the module dependencies both at compile time and runtime. The JDK itself is modularized for Java 9. [8] [9]

Related Research Articles

<span class="mw-page-title-main">Java (programming language)</span> Object-oriented programming language

Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let programmers write once, run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need to recompile. Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of the underlying computer architecture. The syntax of Java is similar to C and C++, but has fewer low-level facilities than either of them. The Java runtime provides dynamic capabilities that are typically not available in traditional compiled languages. As of 2019, Java was one of the most popular programming languages in use according to GitHub, particularly for client–server web applications, with a reported 9 million developers.

In computing, a namespace is a set of signs (names) that are used to identify and refer to objects of various kinds. A namespace ensures that all of a given set of objects have unique names so that they can be easily identified.

Java Platform, Standard Edition is a computing platform for development and deployment of portable code for desktop and server environments. Java SE was formerly known as Java 2 Platform, Standard Edition (J2SE).

<span class="mw-page-title-main">JAR (file format)</span> Java archive file format

A JAR file is a package file format typically used to aggregate many Java class files and associated metadata and resources into one file for distribution.

In computer programming, a global variable is a variable with global scope, meaning that it is visible throughout the program, unless shadowed. The set of all global variables is known as the global environment or global state. In compiled languages, global variables are generally static variables, whose extent (lifetime) is the entire runtime of the program, though in interpreted languages, global variables are generally dynamically allocated when declared, since they are not known ahead of time.

<span class="mw-page-title-main">Swing (Java)</span> Java-based GUI toolkit

Swing is a GUI widget toolkit for Java. It is part of Oracle's Java Foundation Classes (JFC) – an API for providing a graphical user interface (GUI) for Java programs.

A Perl module is a discrete component of software for the Perl programming language. Technically, it is a particular set of conventions for using Perl's package mechanism that has become universally adopted.

This article compares two programming languages: C# with Java. While the focus of this article is mainly the languages and their features, such a comparison will necessarily also consider some features of platforms and libraries. For a more detailed comparison of the platforms, see Comparison of the Java and .NET platforms.

<span class="mw-page-title-main">Java syntax</span> Set of rules defining correctly structured program

The syntax of Java is the set of rules defining how a Java program is written and interpreted.

In the Java computer programming language, an annotation is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and Java packages may be annotated. Like Javadoc tags, Java annotations can be read from source files. Unlike Javadoc tags, Java annotations can also be embedded in and read from Java class files generated by the Java compiler. This allows annotations to be retained by the Java virtual machine at run-time and read via reflection. It is possible to create meta-annotations out of the existing ones in Java.

Classpath is a parameter in the Java Virtual Machine or the Java compiler that specifies the location of user-defined classes and packages. The parameter may be set either on the command-line, or through an environment variable.

This comparison of programming languages compares the features of language syntax (format) for over 50 computer programming languages.

The Java language has undergone several changes since JDK 1.0 as well as numerous additions of classes and packages to the standard library. Since J2SE 1.4, the evolution of the Java language has been governed by the Java Community Process (JCP), which uses Java Specification Requests (JSRs) to propose and specify additions and changes to the Java platform. The language is specified by the Java Language Specification (JLS); changes to the JLS are managed under JSR 901. In September 2017, Mark Reinhold, chief Architect of the Java Platform, proposed to change the release train to "one feature release every six months" rather than the then-current two-year schedule. This proposal took effect for all following versions, and is still the current release schedule.

The Java Class Library (JCL) is a set of dynamically loadable libraries that Java Virtual Machine (JVM) languages can call at run time. Because the Java Platform is not dependent on a specific operating system, applications cannot rely on any of the platform-native libraries. Instead, the Java Platform provides a comprehensive set of standard class libraries, containing the functions common to modern operating systems.

The Java Platform Module System specifies a distribution format for collections of Java code and associated resources. It also specifies a repository for storing these collections, or modules, and identifies how they can be discovered, loaded and checked for integrity. It includes features such as namespaces with the aim of fixing some of the shortcomings in the existing JAR format, especially the JAR Hell, which can lead to issues such as classpath and class loading problems.

This comparison of programming languages compares how object-oriented programming languages such as C++, Java, Smalltalk, Object Pascal, Perl, Python, and others manipulate data structures.

The Java Development Kit (JDK) is a distribution of Java Technology by Oracle Corporation. It implements the Java Language Specification (JLS) and the Java Virtual Machine Specification (JVMS) and provides the Standard Edition (SE) of the Java Application Programming Interface (API). It is derivative of the community driven OpenJDK which Oracle stewards. It provides software for working with Java applications. Examples of included software are the virtual machine, a compiler, performance monitoring tools, a debugger, and other utilities that Oracle considers useful for a Java programmer.

<span class="mw-page-title-main">Abstract Window Toolkit</span> Java-based GUI toolkit

The Abstract Window Toolkit (AWT) is Java's original platform-dependent windowing, graphics, and user-interface widget toolkit, preceding Swing. The AWT is part of the Java Foundation Classes (JFC) — the standard API for providing a graphical user interface (GUI) for a Java program. AWT is also the GUI toolkit for a number of Java ME profiles. For example, Connected Device Configuration profiles require Java runtimes on mobile telephones to support the Abstract Window Toolkit.

The Java Packager tool is a tool included in the JDK which allows to package Java applications from the command line. It is an alternative to using other packaging tools like Apache Ant.

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 and Modules". docs.oracle.com. Retrieved 2021-12-10.
  3. "Chapter 7. Packages". Docs.oracle.com. Retrieved 2013-09-15.
  4. "Creating and Using Packages (The Java™ Tutorials > Learning the Java Language > Packages)". docs.oracle.com.
  5. "Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)". docs.oracle.com.
  6. Code Conventions for the Java Programming Language: 9. Naming Conventions
  7. "Packages". docs.oracle.com.
  8. "JDK Module Summary". Oracle Corporation. 2015-10-23. Archived from the original on 2015-12-08. Retrieved 2015-11-29.
  9. "Understanding Java 9 Modules". Oracle Corporation. October 1, 2017. Retrieved 2022-10-04.