Copyright © 1995-1999, Sun Microsystems, Inc. All Rights Reserved. Used by permission.
Revised April 20, 1999. Format and navigation by Martin Rinehart, www.MartinRinehart.com, May 31, 2005.
Files longer than 2000 lines are cumbersome and should be avoided.
For an example of a Java program properly formatted, see "Java Source File Example" on page 19.
Java source files have the following ordering:
3.1 Java Source Files
Each Java source file contains a single public class or interface. When private classes and interfaces are associated with a public class, you can put them in the same source file as the public class. The public class should be the first class or interface in the file.
3.1.1 Beginning Comments
All source files should begin with a c-style comment that lists the class name, version information, date, and copyright notice:
/*
* Classname
*
* Version information
*
* Date
*
* Copyright notice
*/
| Section # | Part of Class/Interface Declaration | Notes |
|---|---|---|
| 1 | Class/interface documentation comment (/**...*/) | See "Documentation Comments" on page 9 for information on what should be in this comment. |
| 2 | class or interface statement | |
| 3 | Class/interface implementation comment (/*...*/), if necessary | This comment should contain any class-wide or interface-wide information that wasn't appropriate for the class/interface documentation comment. |
| 4 | Class (static) variables | First the public class variables, then the protected, then package level (no access modifier), and then the private. |
| 5 | Instance variables | First public, then protected, then package level (no access modifier), and then private. |
| 6 | Constructors | |
| 7 | Methods | These methods should be grouped by functionality rather than by scope or accessibility. For example, a private class method can be in between two public instance methods. The goal is to make reading and understanding the code easier. |