📄 codeconventions.doc2.html
字号:
<html><head><title></title></head><body bgcolor=#ffffff> <a href="CodeConvTOC.doc.html">[Contents]</a> <a href="CodeConventions.doc1.html">[Prev]</a> <a href="CodeConventions.doc3.html">[Next]</a><hr><br> <a name="3043"> </a><h2 align="center">3 - File Organization</h2><p><a name="3044"> </a>A file consists of sections that should be separated by blank lines and an optional comment identifying each section.</p><p><a name="11682"> </a>Files longer than 2000 lines are cumbersome and should be avoided.</p><p><a name="33974"> </a>For an example of a Java program properly formatted, see <a href="CodeConventions.doc10.html#182">"Java Source File Example" on page 19</a>.</p><a name="11684"> </a><h3> 3.1 Java Source Files</h3><p><a name="215"> </a>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.</p><p><a name="268"> </a>Java source files have the following ordering:</p><ul><a name="508"> </a><li>Beginning comments (see <a href="CodeConventions.doc2.html#3441">"Beginning Comments" on page 4</a>)<a name="515"> </a><li>Package and Import statements<a name="509"> </a><li>Class and interface declarations (see <a href="CodeConventions.doc2.html#1852">"Class and Interface Declarations" on page 4</a>)</ul><a name="3441"> </a><h4>3.1.1 Beginning Comments</h4><p><a name="9333"> </a>All source files should begin with a c-style comment that lists the class name, version information, date, and copyright notice:</p><blockquote><pre>/* * Classname * * Version information * * Date * * Copyright notice */</pre></blockquote><a name="277"> </a><h4>3.1.2 Package and Import Statements</h4><p><a name="1853"> </a>The first non-comment line of most Java source files is a <code>package</code> statement. After that, <code>import</code> statements can follow. For example:</p><blockquote><pre>package java.awt;import java.awt.peer.CanvasPeer;<p></pre></blockquote>Note: The first component of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981. </p><a name="1852"> </a><h4>3.1.3 Class and Interface Declarations</h4><p><a name="222"> </a>The following table describes the parts of a class or interface declaration, in the order that they should appear. See <a href="CodeConventions.doc10.html#182">"Java Source File Example" on page 19</a> for an example that includes comments. <Table Border="3"><caption></caption><tr><th><br><p><a name="3127"> </a>  </p><a name="643"> </a><h2> </h2></th><th><br><a name="650"> </a><h2> Part of Class/Interface Declaration</h2></th><th><br><a name="684"> </a><h2> Notes</h2></th></tr><tr><td><br><p><a name="690"> </a> 1</p></td><td><br><p><a name="693"> </a> Class/interface documentation comment (<code>/**...*/</code>) </p></td><td><br><p><a name="701"> </a> See <a href="CodeConventions.doc4.html#16838">"Documentation Comments" on page 9</a> for information on what should be in this comment.</p></td></tr><tr><td><br><p><a name="718"> </a> 2</p></td><td><br><p><a name="719"> </a> <code>class</code> or <code>interface</code> statement</p></td><td><br><p><a name="3129"> </a>    </p><p><a name="720"> </a> </p></td></tr><tr><td><br><p><a name="724"> </a> 3</p></td><td><br><p><a name="726"> </a> Class/interface implementation comment (<code>/*...*/</code>), if necessary </p></td><td><br><p><a name="744"> </a> This comment should contain any class-wide or interface-wide information that wasn't appropriate for the class/interface documentation comment.</p></td></tr><tr><td><br><p><a name="748"> </a> 4</p></td><td><br><p><a name="749"> </a> Class (<code>static</code>) variables</p></td><td><br><p><a name="750"> </a> First the <code>public</code> class variables, then the <code>protected</code>, then package level (no access modifier), and then the <code>private</code>.</p></td></tr><tr><td><br><p><a name="751"> </a> 5</p></td><td><br><p><a name="752"> </a> Instance variables</p></td><td><br><p><a name="753"> </a> First <code>public</code>, then <code>protected</code>, then package level (no access modifier), and then <code>private</code>.</p></td></tr><tr><td><br><p><a name="754"> </a> 6</p></td><td><br><p><a name="755"> </a> Constructors</p></td><td><br><p><a name="3151"> </a>  </p><p><a name="756"> </a> </p></td></tr><tr><td><br><p><a name="757"> </a> 7</p></td><td><br><p><a name="758"> </a> Methods</p></td><td><br><p><a name="759"> </a> 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.</p></td></tr></Table><Table><tr><td></Table> </p><hr><br> <a href="CodeConvTOC.doc.html">[Contents]</a> <a href="CodeConventions.doc1.html">[Prev]</a> <a href="CodeConventions.doc3.html">[Next]</a> <hr><br><i><a href="Copyright.doc.html">Copyright</a> © 1995-1999, Sun Microsystems, Inc. All rightsreserved.</i><!-- Last updated: Thu Apr 15 09:03:50 1999 --></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -