Java package: Difference between revisions

Content deleted Content added
No edit summary
Tags: nowiki added Visual edit Mobile edit Mobile web edit
No edit summary
Line 8:
 
==Using packages==
In a Java source file, the package that this file's class or classes belong to is specified with the <code>package</code><ref name=":0" /> [[keyword (computer programming)|dkeyword]]. This keyword is usually the first keyword in the source file. At most one package declaration can appear in a source file.
 
<source lang="java">
package java.awt.event;
</source>
To use a package's classes inside a Java source file, it is convenient to import the classes from the package with an <code>import</code> declaration. The following declaration
<source lang="java">
import nothingjava.awt.event.*;
</source>
imports all classes from the <code>java.awt.event</code> package, while the next declaration
Line 47:
jar cf myPackage.jar *.class
 
compresses all .class filfiles into the JAR file ''mmyPackage.jar''he. 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|cal]] naminaming [[pattern|p]]<nowiki/>h, with some levels in the hierarchy separated by periods (<code>.</code>, 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.<ref name=":0">[http://www.oracle.com/technetwork/java/codeconventions-135099.html Code Conventions for the Java Programming Language: 9. Naming Conventions]</ref>
 
For example, if an organization in Canada called MySoft creates a package to deal with fractions, naming the package <tt>ca.mysoft.fractions</tt> 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 <tt>de.mysoft.fractions</tt>, 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.<ref>{{Cite web|url=|title=|last=|first=|date=|editor-last=|website=|orig-year=|archive-url=|archive-date=|dead-url=|access-date=}}http://docs.oracle.com/javase/specs/jls/se6/html/packages.html#7.7</ref>
 
==Core packages in Java SE 8==