Objects
Classes & Java Files
All java source code text is held in clear text java files, which have a .java extension. Compiled into byte form they have an extension of .class.
The structure of a java file tends to be :
package <package name>
import <import file names>
<class definition>
Packages
Packages are a convenient way to group java projects together in to a namespace. All the class files for an application are typically bundled into a jar file using the package structure, allowing for faster distribution of the java application.
A package may used to create a user namespace or enable access to 3rd party classes. When a particular set of classes is needed the package is listed and then the import statement calls the particular package as needed.
Practically this is represented by a file system structure that mirrors the namespace breakdown. For example a package / namespace of org.apache.harmony leads to a file structure of
/org/apache/harmony
A source code example would be:
package java.awt.event; allows access to java.awt.event
import java.awt.event.*; imports all the classes from java.awt.event
import java.awt.event.ActionEvent; imports the classes from java.awt.event.ActionEvent
A full explanation of packages is available here
Common Extensions
There are a number of commonly used extensions in the java world. Some of the more common ones are shown below:
| Extension | Content | Description |
|---|---|---|
| .java | Source Code | Holds the plain text of the java code |
| .class | Byte Code | Holds the compiled code derived from the plain text |
| .jar | Java Archive | Holds all the class files for a java application. A JAR file is used for aggregating many files into one. It is generally used to distribute Java classes and associated metadata. |
| .war | Web Archive | Holds the complete web application class files in one archive.a JAR file used to distribute a collection of JavaServer Pages, servlets, Java classes, XML files, tag libraries and static Web pages (HTML and related files) that together constitute a Web application. |
| .ear | Enterprise Archive | Holds a complete application for deployment to a J2EE server. An EAR file is a standard JAR file with an .ear extension, with one or more entries representing the modules of the application, and a metadata directory called META-INF which contains one or more deployment descriptors |
Java source code is held in files with the extension .java as a plain text file. When it is compiled into byte code it acquires the extension