Pragmatic Programmer Issues

Packages – Java Specification chapter seven

Today I cover chapter seven. Java programs are set of packages. Packages prevent name conflicts.

  • Packages has two kind of members the first is subpackage the second one is top level class or interface.
  • In the same package it is forbidden to have two members with the same name. So in package java.lang when we have ref subpackage then we can’t have ref class or interface definition and vice versa.
  • I was very surprised when I read this, so I quote some paragraphs here. It amazed me:

    The packages may be stored in a local file system in simple implementations of the Java platform. Other implementations may use a distributed file system or some form of database to store source and/or binary code.

    Such a database must not impose the optional restriction on compilation unit in file-based implementations. For example, a system that uses a database to store packages may not enforce a maximum of one public class or interface per compilation unit.

  • The simple file strategy just map every package on directory and compilation unit on file. One thing to mention here is what we can do when package or compilation unit has name with character that can not correctly use on file system. As convention java use some character as escape and choose from non valid identifier character. I try to get this situation on my mac but without success.
  • Compilation unit consist from three parts which are optional
    • package declaration
    • import declaration
    • top level type declaration – I can believe in that so I check this and it is true, there can be no class in compilation unit empty compilation unit
  • The keyword package may optionally be preceded by annotation modifiers, annotation must have annotation.Element.Type.PACKAGE otherwise compiltime error occurs.
  • Package annotation is placed in package-info.java file in directory containing the source files for the package. Technically package-info.java can contain the source code for one or more package-private classes.
  • It is recommended that if package-info.java exists than it takes the place of package.html and should contains javadoc for this package.
  • Compilation unit that has no package declaration is part of an unnamed package. Unnamed package cannot have subpackages.
  • Implementation of Java platform must support AT LEAST one unnamed package, and it MAY support more than one. So we should be aware that two classes without package has different access then package to themselves.
  • Single Type Import Declaration import single type by giving its canonical name. Once imported the name can be used without qualification also in parameterized type. If two single type import declaration attempts to import type with the sam name, we get compiletime error unless they are the same.
  • Type Import on Demand allows all accessible types declared in the package to be imported as needed.
  • Type Import on Demand NEVER causes any other declaration to be shadowed (Single Type Import shadows name declared in compilation unit).
  • Static imports are analogous to normal import but they import static members.
  • Each compilation unit import automatically java.lang.*
  • Top Level Type both class and interface can have one of the following access modifiers: protected, private, static. In this case compiletime error “modifier …….. not allowed here
  • We should consider to make package names unique, and one of workaround is use of ClassLoader to isolate packages with the same name.

Categories