Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

Chapter 4. Access Control > Java Source File Structure - Pg. 104

104 CHAPTER 4: ACCESS CONTROL 4.1 Java Source File Structure The structure of a skeletal Java source file is depicted in Figure 4.1. A Java source file can have the following elements that, if present, must be specified in the fol- lowing order: 1. 2. An optional package declaration to specify a package name. Packages are dis- cussed in Section 4.2. Zero or more import declarations. Since import declarations introduce type or static member names in the source code, they must be placed before any type declarations. Both type and static import statements are discussed in Section 4.2. Any number of top-level type declarations. Class, enum, and interface declara- tions are collectively known as type 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 type declarations can be defined in any order. Technically, a source file need not have any such declaration, but that is hardly useful. The JDK imposes the restriction that at the most one public class declaration 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 , the file name must be . Classes are discussed in Section 3.1, p. 40, and interfaces are discussed in Sec- 3.