Java Quick Reference
  Language Fundamentals
  Operators and Assignments
  Flow Control and Exceptions
  Declarations and Access Control
  Garbage Collection
  Overloading and Overriding
  Threads
  The java.lang Package
  The java.util Package
  The java.awt Package
  The java.io Package
  References
  Miscellaneous Notes
  Tips & Traps
  Mock Exams

Language Fundamentals - Package Declarations

Syntax

    package packageName;
  • packages provide a naming context and an organizational structure for Java compilation units
  • package names are hierarchical with component names separated by dots (JPL pg 25)
  • the standard convention for package naming is to use the reversed internet domain name of whoever's creating the package. For example:
        com.sun.java.awt        // Sun packages
        com.ibm.utils           // IBM packages
        com.acme.tools          // Acme company packages
  • the package naming structure directly maps to a directory structure. For example, if you were developing the Acme company packages your compilation units for the com.acme.tools package would be in:
    directory.............. com
      subdirectory...........acme
        subdirectory..........tools
  • the Java compiler uses a combination of the CLASSPATH and package name to locate the source file
  • host systems may store packages in databases (JLS §7.2.2)
  • if used, it must be the first statement in the source code file
  • you can not declare more than one
  • the package naming structure is for ease of organization only, it does not confer a special relationship (JLS §7.1) ie
    There is no special relationship between the packages:
    
            com.acme.tools,         and
            com.acme.utils
            
    The fact that they share a common subpackage, acme,
    has no meaning in terms of a types scope.

Unnamed packages (JLS §7.4.2)

  • if no package declaration is found, the class or interface is made part of an unnamed package
  • every implementation of Java must provide for at least one unnamed package
  • most systems allow for one unnamed package per directory
Source Package Import Class Interface Constructors
  Methods main() Identifiers Keywords Defaults Arrays
  Primitives # Literals char Literal String Literals Class Literals