Java package: Difference between revisions

Content deleted Content added
No edit summary
Added {{expert}}; some reorganization; added links to API Javadoc; expanded explanation of using packages
Line 1:
{{expert}}
Java Source[[source Filesfile]]s belonging to the same category or provide similar functionality can be packed into a Java Packagepackage. Java Packagespackages are stored in compressed files called [[Jar (file format)|jarJAR]] Filesfiles. Inside the package the files can be stored hierachically, therefore it is possible to combine several subdirectories into one package.
 
*A package provides a unique [[namespace|namespace]] for the types it contains.
Line 6 ⟶ 7:
**[[Class (computer science)|Classes]]
**[[interface (computer science)|Interfaces]]
**[[Enumeration|Enumerated Types]]
**[[Annotation]]s
**Annotations
 
== Using packages ==
To use a package inside a Java Source File, it must be imported with the import statement,
e.g. import java.awt.event.*
 
To use a package inside a Java source file, it is convenient to import the classes from the package with an <code>import</code> statement. The statement
This imports all files from the directory java/awt/event
 
e.g. import java.awt.event.*;
==Packages in Java 1.5==
 
imports all classes from the <code>java.awt.event</code> package, while
* java.lang, java.util: These packages provide basic language functionality like regular expressions, the Reflection API
* java.math: provides mathematical functions
* New I/O: the new I/O framework for Java
* I/O: File Operations
* Networking: Networking Operations, Sockets, DNS lookups,...
* Security: key generation, encryption and decryption
* Java Database Connectivity (JDBC): to access databases
* Java AWT: set of packages for GUI
* Java Swing: native Java GUI components
 
package import java.awt.event.ActionEvent;
== Creation of Jar Files==
 
imports only the <code>ActionEvent</code> class from the package. After either of these import statements, the <code>ActionEvent</code> class can be referenced using its simple class name
In the Java Source Files the package the file shall belong to must be specified with the package keyword, e.g.
package java.awt.event
 
ActionEvent myEvent;
Jar Files are created with the jar command-line utility. The command
 
Classes can also be used directly without an import statement by using the fully-qualified name of the class. For example,
jar cf myPackage.jar *.class
 
java.awt.event.ActionEvent myEvent;
compresses all *.class files into the Jar File myPackage.tar.
 
doesn't require a preceding import statement.
 
== Creation of JarJAR Files ==
 
In the Java Sourcesource Filesfiles the package the file shall belongbelongs to must beis specified with the <code>package</code> [[keyword, e.g]].
 
package java.awt.event;
 
JarJAR Files are created with the jar command-line utility. The command
 
jar cf myPackage.jar *.class
 
compresses all *.class files into the JarJAR Filefile ''myPackage.tar''.
 
== Packages in JavaJ2SE 1.5 ==
 
* {{Javadoc:SE|package=java.lang|java/lang}}, {{Javadoc:SE|package=java.util|java/util}}: These packagespackage hierarchies provide basic language functionality like types, [[regular expressionsexpression]]s, the Reflection API
* {{Javadoc:SE|package=java.math|java/math}}: provides mathematical functions
* {{Javadoc:SE|package=java.io|java/io}}: file operations
* New I{{Javadoc:SE|package=java.nio|java/Onio}}: the new[[New I/O]] framework for Java
* Networking{{Javadoc:SE|package=java.net|java/net}}: Networking Operations, Sockets, DNS lookups, ...
* Security{{Javadoc:SE|package=java.security|java/security}}: key generation, encryption and decryption
* {{Javadoc:SE|package=java.sql|java/sql}}: [[Java Database Connectivity]] (JDBC): to access databases
* {{Javadoc:SE|package=java.awt|java/awt}}: basic hierarchy of packages for native GUI components
* {{Javadoc:SE|package=javax.swing|javax/swing}}: hierarchy of packages for platform-independent rich GUI components
 
== External links ==
 
* {{Javadoc:SE}}
 
{{compu-lang-stub}}