PACKAGES (3)
PACKAGES (3)
TOP LEVEL TYPE DECLARATIONS:
Top level type declarations declare a top level class type or a top level interface type.
Accessibility: the default accessibility (package-private) applies to this declaration, but if the declaration is public, access is granted to the type by codes from other packages.
Scope: the scope of this declaration is all type declarations in the package in which the top level type is declared.
For instance, where the package has name P and the type is named T, therefore , the fully qualified name is P.T, otherwise the fully qualified name is T, i.e where the type is in an unnamed package.
Note:
1. a restriction can always be enforced by a host system that if a type is not associated with a type name plus an extension (.java or .jav) and either:
a. the type is referred to by code in other compilation units of the package in which the type is declared
b. or the type is declared public (quite sure this’ your piece of cake!)
is true, then the host system should return a compile time error. So, there must be at most one such type per compilation unit.
In a database storage, this restriction might not be imposed.
2. a compile time error occurs if the name of a top level type appears as the name of any other top level class or interface type declared in the same package.
3. also, if (2) is also declared as a type by a single-type-import declaration in the compilation unit containing the type declaration, a compile time error occurs.
Please, see the examples in §7.6 of the jls; they are very instructive and I don’t want to create a redundancy.
UNIQUE PACKAGE NAMES:
Unique package names are essentially necessary when a package is not for local or casual usage but to be widely distributed for easy and automatic installation and cataloguing of packages.
Suggestions of usage:
1. unique package names are usually formed form internet domain names of the organization one belongs to. Example: if your internet domain is mydomain.com, these are usually used to derive unique package names. You reverse this name, component by component to obtain, com.mydomain and use this as a prefix for your package names. Usally, a convention is developed within organizations on how to administer package names using this domain name as base.
Where the internet domain name is not a valid package name for your case you can do the following:
a. where the domain name contains a hyphen or any other special character not allowed in an identifier, convert it into an underscore _ .
b. if any resulting package name components are keywords, then append an underscore to them.
c. If any of the resulting package name components start with a digit, or any other character not allowed as an initial character of an identifier, have an underscore prefixed to the component.
2. the first component of unique package names is always written in all lower-case ASCII letters and should be a top level domain name (currently com, edu, mil, net and org) or one of the English two-letter codes identifying countries as specified in ISO standard 3166, 1981.
com.mydomain.myjavafiles.me
edu.myschool.myschoolfiles._us
3. the name of a package is not meant to imply where the package is stored within the internet. The suggested convention above merely a convenience to piggyback a package naming convention on top of an existing, widely known unique name registry instead of having to create a separate registry for package names.
No comments:
Post a Comment