2.5 Java Source File Structure


The structure of a skeletal Java source file is depicted in Figure 2.2. A Java source file can have the following elements that, if present, must be specified in the following order:

  1. An optional package declaration to specify a package name . Packages are discussed in Section 4.6.

  2. Zero or more import declarations. Since import declarations introduce class and interface names in the source code, they must be placed before any type declarations. The import statement is discussed in Section 4.6.

  3. Any number of top-level class and interface declarations. Since these declarations belong to the same package, they are said to be defined at the top level , which is the package level.

    The classes and interfaces can be defined in any order. Class and interface declarations are collectively known as type declarations . Technically, a source file need not have any such definitions, but that is hardly useful.

    The Java 2 SDK imposes the restriction that at the most one public class definition per source file can be defined. If a public class is defined, the file name must match this public class. If the public class name is NewApp , then the file name must be NewApp.java .

    Classes are discussed in Section 4.2, and interfaces are discussed in Section 6.4.

Figure 2.2. Java Source File Structure

graphics/02fig02.gif

Note that except for the package and the import statements, all code is encapsulated in classes and interfaces. No such restriction applies to comments and white space.



A Programmer[ap]s Guide to Java Certification
A Programmer[ap]s Guide to Java Certification
ISBN: 201596148
EAN: N/A
Year: 2003
Pages: 284

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net