1<!DOCTYPE html> 2<html> 3<head> 4<meta http-equiv="content-type" content="text/html; charset=UTF-8"> 5<title>Google Java Style Guide</title> 6<link rel="stylesheet" type="text/css" href="javaguide.css"> 7<script language="javascript" src="include/styleguide.js"></script> 8<link rel="shortcut icon" type="image/x-icon" href="https://www.google.com/favicon.ico" /> 9<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script> 10</head> 11<body onload="initStyleGuide();"> 12<div id="content"> 13<h1>Google Java Style Guide</h1> 14<div class="vertical_toc" id="tocDiv"></div> 15 16<div class="main_body"> 17 18<h2 id="s1-introduction">1 Introduction</h2> 19 20<p>This document serves as the <strong>complete</strong> definition of Google's coding standards for 21source code in the Java™ Programming Language. A Java source file is described as being <em>in 22Google Style</em> if and only if it adheres to the rules herein.</p> 23 24<p>Like other programming style guides, the issues covered span not only aesthetic issues of 25formatting, but other types of conventions or coding standards as well. However, this document 26focuses primarily on the <strong>hard-and-fast rules</strong> that we follow universally, and 27avoids giving <em>advice</em> that isn't clearly enforceable (whether by human or tool). 28</p> 29 30 31 32<h3 id="s1.1-terminology">1.1 Terminology notes</h3> 33 34<p>In this document, unless otherwise clarified:</p> 35 36<ol> 37 <li>The term <em>class</em> is used inclusively to mean an "ordinary" class, enum class, 38 interface or annotation type (<code class="prettyprint lang-java">@interface</code>).</li> 39 40 <li>The term <em>member</em> (of a class) is used inclusively to mean a nested class, field, 41 method, <em>or constructor</em>; that is, all top-level contents of a class except initializers 42 and comments. 43 44 </li><li>The term <em>comment</em> always refers to <em>implementation</em> comments. We do not 45 use the phrase "documentation comments", instead using the common term "Javadoc."</li> 46</ol> 47 48<p>Other "terminology notes" will appear occasionally throughout the document.</p> 49 50<h3 id="s1.2-guide-notes">1.2 Guide notes</h3> 51 52<p>Example code in this document is <strong>non-normative</strong>. That is, while the examples 53are in Google Style, they may not illustrate the <em>only</em> stylish way to represent the 54code. Optional formatting choices made in examples should not be enforced as rules.</p> 55 56 57<h2 id="s2-source-file-basics">2 Source file basics</h2> 58 59<h3 id="s2.1-file-name">2.1 File name</h3> 60 61<p>The source file name consists of the case-sensitive name of the top-level class it contains 62(of which there is <a href="#s3.4.1-one-top-level-class">exactly one</a>), plus the 63<code>.java</code> extension.</p> 64 65<h3 id="s2.2-file-encoding">2.2 File encoding: UTF-8</h3> 66 67<p>Source files are encoded in <strong>UTF-8</strong>.</p> 68 69<h3 id="s2.3-special-characters">2.3 Special characters</h3> 70 71<h4 id="s2.3.1-whitespace-characters">2.3.1 Whitespace characters</h4> 72 73<p>Aside from the line terminator sequence, the <strong>ASCII horizontal space 74character</strong> (<strong>0x20</strong>) is the only whitespace character that appears 75anywhere in a source file. This implies that:</p> 76 77<ol> 78 <li>All other whitespace characters in string and character literals are escaped.</li> 79 80 <li>Tab characters are <strong>not</strong> used for indentation.</li> 81</ol> 82 83<h4 id="s2.3.2-special-escape-sequences">2.3.2 Special escape sequences</h4> 84 85<p>For any character that has a 86<a href="http://docs.oracle.com/javase/tutorial/java/data/characters.html"> 87 special escape sequence</a> 88(<code class="prettyprint lang-java">\b</code>, 89<code class="prettyprint lang-java">\t</code>, 90<code class="prettyprint lang-java">\n</code>, 91<code class="prettyprint lang-java">\f</code>, 92<code class="prettyprint lang-java">\r</code>, 93<code class="prettyprint lang-java">\"</code>, 94<code class="prettyprint lang-java">\'</code> and 95<code class="prettyprint lang-java">\\</code>), that sequence 96is used rather than the corresponding octal 97(e.g. <code class="badcode">\012</code>) or Unicode 98(e.g. <code class="badcode">\u000a</code>) escape.</p> 99 100<h4 id="s2.3.3-non-ascii-characters">2.3.3 Non-ASCII characters</h4> 101 102<p>For the remaining non-ASCII characters, either the actual Unicode character 103(e.g. <code class="prettyprint lang-java">∞</code>) or the equivalent Unicode escape 104(e.g. <code class="prettyprint lang-java">\u221e</code>) is used. The choice depends only on 105which makes the code <strong>easier to read and understand</strong>, although Unicode escapes 106outside string literals and comments are strongly discouraged.</p> 107 108<p class="tip"><strong>Tip:</strong> In the Unicode escape case, and occasionally even when actual 109Unicode characters are used, an explanatory comment can be very helpful.</p> 110 111<p>Examples:</p> 112 113<table> 114 <tbody><tr> 115 <th>Example</th> 116 <th>Discussion</th> 117 </tr> 118 119 <tr> 120 <td><code class="prettyprint lang-java">String unitAbbrev = "μs";</code></td> 121 <td>Best: perfectly clear even without a comment.</td> 122 </tr> 123 124 <tr> 125 <td><code class="prettyprint lang-java">String unitAbbrev = "\u03bcs"; // "μs"</code></td> 126 <td>Allowed, but there's no reason to do this.</td> 127 </tr> 128 129 <tr> 130 <td><code class="prettyprint lang-java">String unitAbbrev = "\u03bcs"; 131 // Greek letter mu, "s"</code></td> 132 <td>Allowed, but awkward and prone to mistakes.</td> 133 </tr> 134 135 <tr> 136 <td><code class="badcode">String unitAbbrev = "\u03bcs";</code></td> 137 <td>Poor: the reader has no idea what this is.</td> 138 </tr> 139 140 <tr> 141 <td><code class="prettyprint lang-java">return '\ufeff' + content; 142 // byte order mark</code></td> 143 <td>Good: use escapes for non-printable characters, and comment if necessary.</td> 144 </tr> 145</tbody></table> 146 147<p class="tip"><strong>Tip:</strong> Never make your code less readable simply out of fear that 148some programs might not handle non-ASCII characters properly. If that should happen, those 149programs are <strong>broken</strong> and they must be <strong>fixed</strong>.</p> 150 151 152<a name="filestructure"></a> 153<h2 id="s3-source-file-structure">3 Source file structure</h2> 154 155<div> 156<p>A source file consists of, <strong>in order</strong>:</p> 157 158<ol> 159 <li>License or copyright information, if present</li> 160 <li>Package statement</li> 161 <li>Import statements</li> 162 <li>Exactly one top-level class</li> 163</ol> 164</div> 165 166<p><strong>Exactly one blank line</strong> separates each section that is present.</p> 167 168<h3 id="s3.1-copyright-statement">3.1 License or copyright information, if present</h3> 169 170<p>If license or copyright information belongs in a file, it belongs here.</p> 171 172 173 174<h3 id="s3.2-package-statement">3.2 Package statement</h3> 175 176<p>The package statement is <strong>not line-wrapped</strong>. The column limit (Section 4.4, 177<a href="#s4.4-column-limit">Column limit: 100</a>) does not apply to package statements.</p> 178 179<a name="imports"></a> 180<h3 id="s3.3-import-statements">3.3 Import statements</h3> 181 182<h4 id="s3.3.1-wildcard-imports">3.3.1 No wildcard imports</h4> 183 184<p><strong>Wildcard imports</strong>, static or otherwise, <strong>are not used</strong>.</p> 185 186<h4 id="s3.3.2-import-line-wrapping">3.3.2 No line-wrapping</h4> 187 188<p>Import statements are <strong>not line-wrapped</strong>. The column limit (Section 4.4, 189<a href="#s4.4-column-limit">Column limit: 100</a>) does not apply to import 190statements.</p> 191 192<h4 id="s3.3.3-import-ordering-and-spacing">3.3.3 Ordering and spacing</h4> 193 194<p>Imports are ordered as follows:</p> 195 196<ol> 197 <li>All static imports in a single block.</li> 198 <li>All non-static imports in a single block.</li> 199</ol> 200 201<p>If there are both static and non-static imports, a single blank line separates the two 202blocks. There are no other blank lines between import statements.</p> 203 204<p>Within each block the imported names appear in ASCII sort order. (<strong>Note:</strong> 205this is not the same as the import <em>statements</em> being in ASCII sort order, since '.' 206sorts before ';'.)</p> 207 208 209 210<h4 id="s3.3.4-import-class-not-static">3.3.4 No static import for classes</h4> 211 212<p>Static import is not used for static nested classes. They are imported with 213normal imports.</p> 214 215<h3 id="s3.4-class-declaration">3.4 Class declaration</h3> 216 217<a name="oneclassperfile"></a> 218<h4 id="s3.4.1-one-top-level-class">3.4.1 Exactly one top-level class declaration</h4> 219 220<p>Each top-level class resides in a source file of its own.</p> 221 222<a name="s3.4.2-class-member-ordering"></a> 223<h4 id="s3.4.2-ordering-class-contents">3.4.2 Ordering of class contents</h4> 224 225<p>The order you choose for the members and initializers of your class can have a great effect on 226learnability. However, there's no single correct recipe for how to do it; different classes may 227order their contents in different ways.</p> 228 229<p>What is important is that each class uses <strong><em>some</em> logical order</strong>, which its 230maintainer could explain if asked. For example, new methods are not just habitually added to the end 231of the class, as that would yield "chronological by date added" ordering, which is not a logical 232ordering.</p> 233 234 235 236<a name="overloads"></a> 237<h5 id="s3.4.2.1-overloads-never-split">3.4.2.1 Overloads: never split</h5> 238 239<p>When a class has multiple constructors, or multiple methods with the same name, these appear 240sequentially, with no other code in between (not even private members).</p> 241 242<h2 id="s4-formatting">4 Formatting</h2> 243 244<p class="terminology"><strong>Terminology Note:</strong> <em>block-like construct</em> refers to 245the body of a class, method or constructor. Note that, by Section 4.8.3.1 on 246<a href="#s4.8.3.1-array-initializers">array initializers</a>, any array initializer 247<em>may</em> optionally be treated as if it were a block-like construct.</p> 248 249<a name="braces"></a> 250<h3 id="s4.1-braces">4.1 Braces</h3> 251 252<h4 id="s4.1.1-braces-always-used">4.1.1 Braces are used where optional</h4> 253 254<p>Braces are used with 255<code class="prettyprint lang-java">if</code>, 256<code class="prettyprint lang-java">else</code>, 257<code class="prettyprint lang-java">for</code>, 258<code class="prettyprint lang-java">do</code> and 259<code class="prettyprint lang-java">while</code> statements, even when the 260body is empty or contains only a single statement.</p> 261 262<h4 id="s4.1.2-blocks-k-r-style">4.1.2 Nonempty blocks: K & R style</h4> 263 264<p>Braces follow the Kernighan and Ritchie style 265("<a href="http://www.codinghorror.com/blog/2012/07/new-programming-jargon.html">Egyptian brackets</a>") 266for <em>nonempty</em> blocks and block-like constructs:</p> 267 268<ul> 269 <li>No line break before the opening brace.</li> 270 271 <li>Line break after the opening brace.</li> 272 273 <li>Line break before the closing brace.</li> 274 275 <li>Line break after the closing brace, <em>only if</em> that brace terminates a statement or 276 terminates the body of a method, constructor, or <em>named</em> class. 277 For example, there is <em>no</em> line break after the brace if it is followed by 278 <code class="prettyprint lang-java">else</code> or a comma.</li> 279</ul> 280 281<p>Examples:</p> 282 283<pre class="prettyprint lang-java">return () -> { 284 while (condition()) { 285 method(); 286 } 287}; 288 289return new MyClass() { 290 @Override public void method() { 291 if (condition()) { 292 try { 293 something(); 294 } catch (ProblemException e) { 295 recover(); 296 } 297 } else if (otherCondition()) { 298 somethingElse(); 299 } else { 300 lastThing(); 301 } 302 } 303}; 304</pre> 305 306<p>A few exceptions for enum classes are given in Section 4.8.1, 307<a href="#s4.8.1-enum-classes">Enum classes</a>.</p> 308 309<a name="emptyblocks"></a> 310<h4 id="s4.1.3-braces-empty-blocks">4.1.3 Empty blocks: may be concise</h4> 311 312<p>An empty block or block-like construct may be in K & R style (as described in 313<a href="#s4.1.2-blocks-k-r-style">Section 4.1.2</a>). Alternatively, it may be closed immediately 314after it is opened, with no characters or line break in between 315(<code class="prettyprint lang-java">{}</code>), <strong>unless</strong> it is part of a 316<em>multi-block statement</em> (one that directly contains multiple blocks: 317<code class="prettyprint lang-java">if/else</code> or 318<code class="prettyprint lang-java">try/catch/finally</code>).</p> 319 320<p>Examples:</p> 321 322<pre class="prettyprint lang-java"> // This is acceptable 323 void doNothing() {} 324 325 // This is equally acceptable 326 void doNothingElse() { 327 } 328</pre> 329<pre class="prettyprint lang-java badcode"> // This is not acceptable: No concise empty blocks in a multi-block statement 330 try { 331 doSomething(); 332 } catch (Exception e) {} 333</pre> 334 335<h3 id="s4.2-block-indentation">4.2 Block indentation: +2 spaces</h3> 336 337<p>Each time a new block or block-like construct is opened, the indent increases by two 338spaces. When the block ends, the indent returns to the previous indent level. The indent level 339applies to both code and comments throughout the block. (See the example in Section 4.1.2, 340<a href="#s4.1.2-blocks-k-r-style">Nonempty blocks: K & R Style</a>.)</p> 341 342<h3 id="s4.3-one-statement-per-line">4.3 One statement per line</h3> 343 344<p>Each statement is followed by a line break.</p> 345 346<a name="columnlimit"></a> 347<h3 id="s4.4-column-limit">4.4 Column limit: 100</h3> 348 349<p>Java code has a column limit of 100 characters. A "character" means any Unicode code point. 350Except as noted below, any line that would exceed this limit must be line-wrapped, as explained in 351Section 4.5, <a href="#s4.5-line-wrapping">Line-wrapping</a>. 352</p> 353 354<p class="tip">Each Unicode code point counts as one character, even if its display width is 355greater or less. For example, if using 356<a href="https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms">fullwidth characters</a>, 357you may choose to wrap the line earlier than where this rule strictly requires.</p> 358 359<p><strong>Exceptions:</strong></p> 360 361<ol> 362 <li>Lines where obeying the column limit is not possible (for example, a long URL in Javadoc, 363 or a long JSNI method reference).</li> 364 365 <li><code class="prettyprint lang-java">package</code> and 366 <code class="prettyprint lang-java">import</code> statements (see Sections 367 3.2 <a href="#s3.2-package-statement">Package statement</a> and 368 3.3 <a href="#s3.3-import-statements">Import statements</a>).</li> 369 370 <li>Command lines in a comment that may be cut-and-pasted into a shell.</li> 371</ol> 372 373<h3 id="s4.5-line-wrapping">4.5 Line-wrapping</h3> 374 375<p class="terminology"><strong>Terminology Note:</strong> When code that might otherwise legally 376occupy a single line is divided into multiple lines, this activity is called 377<em>line-wrapping</em>.</p> 378 379<p>There is no comprehensive, deterministic formula showing <em>exactly</em> how to line-wrap in 380every situation. Very often there are several valid ways to line-wrap the same piece of code.</p> 381 382<p class="note"><strong>Note:</strong> While the typical reason for line-wrapping is to avoid 383overflowing the column limit, even code that would in fact fit within the column limit <em>may</em> 384be line-wrapped at the author's discretion.</p> 385 386<p class="tip"><strong>Tip:</strong> Extracting a method or local variable may solve the problem 387without the need to line-wrap.</p> 388 389<h4 id="s4.5.1-line-wrapping-where-to-break">4.5.1 Where to break</h4> 390 391<p>The prime directive of line-wrapping is: prefer to break at a 392<strong>higher syntactic level</strong>. Also:</p> 393 394<ol> 395 <li>When a line is broken at a <em>non-assignment</em> operator the break comes <em>before</em> 396 the symbol. (Note that this is not the same practice used in Google style for other languages, 397 such as C++ and JavaScript.) 398 <ul> 399 <li>This also applies to the following "operator-like" symbols: 400 <ul> 401 <li>the dot separator (<code class="prettyprint lang-java">.</code>)</li> 402 <li>the two colons of a method reference 403 (<code class="prettyprint lang-java">::</code>)</li> 404 <li>an ampersand in a type bound 405 (<code class="prettyprint lang-java"><T extends Foo & Bar></code>)</li> 406 <li>a pipe in a catch block 407 (<code class="prettyprint lang-java">catch (FooException | BarException e)</code>).</li> 408 </ul> 409 </li> 410 </ul> 411 </li> 412 413 <li>When a line is broken at an <em>assignment</em> operator the break typically comes 414 <em>after</em> the symbol, but either way is acceptable. 415 <ul> 416 <li>This also applies to the "assignment-operator-like" colon in an enhanced 417 <code class="prettyprint lang-java">for</code> ("foreach") statement.</li> 418 </ul> 419 </li> 420 421 <li>A method or constructor name stays attached to the open parenthesis 422 (<code class="prettyprint lang-java">(</code>) that follows it.</li> 423 424 <li>A comma (<code class="prettyprint lang-java">,</code>) stays attached to the token that 425 precedes it.</li> 426 427 <li>A line is never broken adjacent to the arrow in a lambda, except that a 428 break may come immediately after the arrow if the body of the lambda consists 429 of a single unbraced expression. Examples: 430<pre class="prettyprint lang-java">MyLambda<String, Long, Object> lambda = 431 (String label, Long value, Object obj) -> { 432 ... 433 }; 434 435Predicate<String> predicate = str -> 436 longExpressionInvolving(str); 437</pre> 438 </li> 439</ol> 440 441<p class="note"><strong>Note:</strong> The primary goal for line wrapping is to have clear 442code, <em>not necessarily</em> code that fits in the smallest number of lines.</p> 443 444<a name="indentation"></a> 445<h4 id="s4.5.2-line-wrapping-indent">4.5.2 Indent continuation lines at least +4 spaces</h4> 446 447<p>When line-wrapping, each line after the first (each <em>continuation line</em>) is indented 448at least +4 from the original line.</p> 449 450<p>When there are multiple continuation lines, indentation may be varied beyond +4 as 451desired. In general, two continuation lines use the same indentation level if and only if they 452begin with syntactically parallel elements.</p> 453 454<p>Section 4.6.3 on <a href="#s4.6.3-horizontal-alignment">Horizontal alignment</a> addresses 455the discouraged practice of using a variable number of spaces to align certain tokens with 456previous lines.</p> 457 458<h3 id="s4.6-whitespace">4.6 Whitespace</h3> 459 460<h4 id="s4.6.1-vertical-whitespace">4.6.1 Vertical Whitespace</h4> 461 462<p>A single blank line appears:</p> 463 464<ol> 465 <li><em>Between</em> consecutive members or initializers of a class: fields, constructors, 466 methods, nested classes, static initializers, and instance initializers. 467 <ul> 468 <li><span class="exception"><strong>Exception:</strong> A blank line between two consecutive 469 fields (having no other code between them) is optional. Such blank lines are used as needed to 470 create <em>logical groupings</em> of fields.</span></li> 471 <li><span class="exception"><strong>Exception:</strong> Blank lines between enum constants are 472 covered in <a href="#s4.8.1-enum-classes">Section 4.8.1</a>.</span></li> 473 </ul> 474 </li> 475 476 <li>Between statements, <em>as needed</em> to organize the code into logical subsections. 477 478 </li><li><em>Optionally</em> before the first member or initializer, or after the last member or 479 initializer of the class (neither encouraged nor discouraged).</li> 480 481 <li>As required by other sections of this document (such as Section 3, 482 <a href="#s3-source-file-structure">Source file structure</a>, and Section 3.3, 483 <a href="#s3.3-import-statements">Import statements</a>).</li> 484</ol> 485 486<p><em>Multiple</em> consecutive blank lines are permitted, but never required (or encouraged).</p> 487 488<h4 id="s4.6.2-horizontal-whitespace">4.6.2 Horizontal whitespace</h4> 489 490<p>Beyond where required by the language or other style rules, and apart from literals, comments and 491Javadoc, a single ASCII space also appears in the following places <strong>only</strong>.</p> 492 493<ol> 494 <li>Separating any reserved word, such as 495 <code class="prettyprint lang-java">if</code>, 496 <code class="prettyprint lang-java">for</code> or 497 <code class="prettyprint lang-java">catch</code>, from an open parenthesis 498 (<code class="prettyprint lang-java">(</code>) 499 that follows it on that line</li> 500 501 <li>Separating any reserved word, such as 502 <code class="prettyprint lang-java">else</code> or 503 <code class="prettyprint lang-java">catch</code>, from a closing curly brace 504 (<code class="prettyprint lang-java">}</code>) that precedes it on that line</li> 505 506 <li>Before any open curly brace 507 (<code class="prettyprint lang-java">{</code>), with two exceptions: 508 <ul> 509 <li><code class="prettyprint lang-java">@SomeAnnotation({a, b})</code> (no space is used)</li> 510 511 <li><code class="prettyprint lang-java">String[][] x = {{"foo"}};</code> (no space is required 512 between <code class="prettyprint lang-java">{{</code>, by item 8 below)</li> 513 </ul> 514 </li> 515 516 <li>On both sides of any binary or ternary operator. This also applies to the following 517 "operator-like" symbols: 518 <ul> 519 <li>the ampersand in a conjunctive type bound: 520 <code class="prettyprint lang-java"><T extends Foo & Bar></code></li> 521 522 <li>the pipe for a catch block that handles multiple exceptions: 523 <code class="prettyprint lang-java">catch (FooException | BarException e)</code></li> 524 525 <li>the colon (<code class="prettyprint lang-java">:</code>) in an enhanced 526 <code class="prettyprint lang-java">for</code> ("foreach") statement</li> 527 528 <li>the arrow in a lambda expression: 529 <code class="prettyprint lang-java">(String str) -> str.length()</code></li> 530 </ul> 531 but not 532 533 <ul> 534 <li>the two colons (<code class="prettyprint lang-java">::</code>) of a method reference, which 535 is written like <code class="prettyprint lang-java">Object::toString</code></li> 536 <li>the dot separator (<code class="prettyprint lang-java">.</code>), which is written like 537 <code class="prettyprint lang-java">object.toString()</code></li> 538 </ul> 539 </li> 540 541 <li>After <code class="prettyprint lang-java">,:;</code> or the closing parenthesis 542 (<code class="prettyprint lang-java">)</code>) of a cast</li> 543 544 <li>On both sides of the double slash (<code class="prettyprint lang-java">//</code>) that 545 begins an end-of-line comment. Here, multiple spaces are allowed, but not required.</li> 546 547 <li>Between the type and variable of a declaration: 548 <code class="prettyprint lang-java">List<String> list</code></li> 549 550 <li><em>Optional</em> just inside both braces of an array initializer 551 <ul> 552 <li><code class="prettyprint lang-java">new int[] {5, 6}</code> and 553 <code class="prettyprint lang-java">new int[] { 5, 6 }</code> are both valid</li> 554 </ul> 555 </li> 556</ol> 557 558This rule is never interpreted as requiring or forbidding additional space at the start or 559end of a line; it addresses only <em>interior</em> space. 560 561<h4 id="s4.6.3-horizontal-alignment">4.6.3 Horizontal alignment: never required</h4> 562 563<p class="terminology"><strong>Terminology Note:</strong> <em>Horizontal alignment</em> is the 564practice of adding a variable number of additional spaces in your code with the goal of making 565certain tokens appear directly below certain other tokens on previous lines.</p> 566 567<p>This practice is permitted, but is <strong>never required</strong> by Google Style. It is not 568even required to <em>maintain</em> horizontal alignment in places where it was already used.</p> 569 570<p>Here is an example without alignment, then using alignment:</p> 571 572<pre class="prettyprint lang-java">private int x; // this is fine 573private Color color; // this too 574 575private int x; // permitted, but future edits 576private Color color; // may leave it unaligned 577</pre> 578 579<p class="tip"><strong>Tip:</strong> Alignment can aid readability, but it creates problems for 580future maintenance. Consider a future change that needs to touch just one line. This change may 581leave the formerly-pleasing formatting mangled, and that is <strong>allowed</strong>. More often 582it prompts the coder (perhaps you) to adjust whitespace on nearby lines as well, possibly 583triggering a cascading series of reformattings. That one-line change now has a "blast radius." 584This can at worst result in pointless busywork, but at best it still corrupts version history 585information, slows down reviewers and exacerbates merge conflicts.</p> 586 587<a name="parentheses"></a> 588<h3 id="s4.7-grouping-parentheses">4.7 Grouping parentheses: recommended</h3> 589 590<p>Optional grouping parentheses are omitted only when author and reviewer agree that there is no 591reasonable chance the code will be misinterpreted without them, nor would they have made the code 592easier to read. It is <em>not</em> reasonable to assume that every reader has the entire Java 593operator precedence table memorized.</p> 594 595<h3 id="s4.8-specific-constructs">4.8 Specific constructs</h3> 596 597<h4 id="s4.8.1-enum-classes">4.8.1 Enum classes</h4> 598 599<p>After each comma that follows an enum constant, a line break is optional. Additional blank 600lines (usually just one) are also allowed. This is one possibility: 601 602</p><pre class="prettyprint lang-java">private enum Answer { 603 YES { 604 @Override public String toString() { 605 return "yes"; 606 } 607 }, 608 609 NO, 610 MAYBE 611} 612</pre> 613 614<p>An enum class with no methods and no documentation on its constants may optionally be formatted 615as if it were an array initializer (see Section 4.8.3.1 on 616<a href="#s4.8.3.1-array-initializers">array initializers</a>).</p> 617 618<pre class="prettyprint lang-java">private enum Suit { CLUBS, HEARTS, SPADES, DIAMONDS } 619</pre> 620 621<p>Since enum classes <em>are classes</em>, all other rules for formatting classes apply.</p> 622 623<a name="localvariables"></a> 624<h4 id="s4.8.2-variable-declarations">4.8.2 Variable declarations</h4> 625 626<h5 id="s4.8.2.1-variables-per-declaration">4.8.2.1 One variable per declaration</h5> 627 628<p>Every variable declaration (field or local) declares only one variable: declarations such as 629<code class="badcode">int a, b;</code> are not used.</p> 630 631<p><strong>Exception:</strong> Multiple variable declarations are acceptable in the header of a 632<code class="prettyprint lang-java">for</code> loop.</p> 633 634<h5 id="s4.8.2.2-variables-limited-scope">4.8.2.2 Declared when needed</h5> 635 636<p>Local variables are <strong>not</strong> habitually declared at the start of their containing 637block or block-like construct. Instead, local variables are declared close to the point they are 638first used (within reason), to minimize their scope. Local variable declarations typically have 639initializers, or are initialized immediately after declaration.</p> 640 641<h4 id="s4.8.3-arrays">4.8.3 Arrays</h4> 642 643<h5 id="s4.8.3.1-array-initializers">4.8.3.1 Array initializers: can be "block-like"</h5> 644 645<p>Any array initializer may <em>optionally</em> be formatted as if it were a "block-like 646construct." For example, the following are all valid (<strong>not</strong> an exhaustive 647list):</p> 648 649<pre class="prettyprint lang-java">new int[] { new int[] { 650 0, 1, 2, 3 0, 651} 1, 652 2, 653new int[] { 3, 654 0, 1, } 655 2, 3 656} new int[] 657 {0, 1, 2, 3} 658</pre> 659 660<h5 id="s4.8.3.2-array-declarations">4.8.3.2 No C-style array declarations</h5> 661 662<p>The square brackets form a part of the <em>type</em>, not the variable: 663<code class="prettyprint lang-java">String[] args</code>, not 664<code class="badcode">String args[]</code>.</p> 665 666<h4 id="s4.8.4-switch">4.8.4 Switch statements</h4> 667 668 669 670<p class="terminology"><strong>Terminology Note:</strong> Inside the braces of a 671<em>switch block</em> are one or more <em>statement groups</em>. Each statement group consists of 672one or more <em>switch labels</em> (either <code class="prettyprint lang-java">case FOO:</code> or 673<code class="prettyprint lang-java">default:</code>), followed by one or more statements (or, for 674the <em>last</em> statement group, <em>zero</em> or more statements).</p> 675 676<h5 id="s4.8.4.1-switch-indentation">4.8.4.1 Indentation</h5> 677 678<p>As with any other block, the contents of a switch block are indented +2.</p> 679 680<p>After a switch label, there is a line break, and the indentation level is increased +2, exactly 681as if a block were being opened. The following switch label returns to the previous indentation 682level, as if a block had been closed.</p> 683 684<a name="fallthrough"></a> 685<h5 id="s4.8.4.2-switch-fall-through">4.8.4.2 Fall-through: commented</h5> 686 687<p>Within a switch block, each statement group either terminates abruptly (with a 688<code class="prettyprint lang-java">break</code>, 689<code class="prettyprint lang-java">continue</code>, 690<code class="prettyprint lang-java">return</code> or thrown exception), or is marked with a comment 691to indicate that execution will or <em>might</em> continue into the next statement group. Any 692comment that communicates the idea of fall-through is sufficient (typically 693<code class="prettyprint lang-java">// fall through</code>). This special comment is not required in 694the last statement group of the switch block. Example:</p> 695 696<pre class="prettyprint lang-java">switch (input) { 697 case 1: 698 case 2: 699 prepareOneOrTwo(); 700 // fall through 701 case 3: 702 handleOneTwoOrThree(); 703 break; 704 default: 705 handleLargeNumber(input); 706} 707</pre> 708 709<p>Notice that no comment is needed after <code class="prettyprint lang-java">case 1:</code>, only 710at the end of the statement group.</p> 711 712<h5 id="s4.8.4.3-switch-default">4.8.4.3 The <code>default</code> case is present</h5> 713 714<p>Each switch statement includes a <code class="prettyprint lang-java">default</code> statement 715group, even if it contains no code.</p> 716 717<p><strong>Exception:</strong> A switch statement for an <code>enum</code> type <em>may</em> omit 718the <code class="prettyprint lang-java">default</code> statement group, <em>if</em> it includes 719explicit cases covering <em>all</em> possible values of that type. This enables IDEs or other static 720analysis tools to issue a warning if any cases were missed. 721 722</p> 723 724<a name="annotations"></a> 725<h4 id="s4.8.5-annotations">4.8.5 Annotations</h4> 726 727<p>Annotations applying to a class, method or constructor appear immediately after the 728documentation block, and each annotation is listed on a line of its own (that is, one annotation 729per line). These line breaks do not constitute line-wrapping (Section 7304.5, <a href="#s4.5-line-wrapping">Line-wrapping</a>), so the indentation level is not 731increased. Example:</p> 732 733<pre class="prettyprint lang-java">@Override 734@Nullable 735public String getNameIfPresent() { ... } 736</pre> 737 738<p class="exception"><strong>Exception:</strong> A <em>single</em> parameterless annotation 739<em>may</em> instead appear together with the first line of the signature, for example:</p> 740 741<pre class="prettyprint lang-java">@Override public int hashCode() { ... } 742</pre> 743 744<p>Annotations applying to a field also appear immediately after the documentation block, but in 745this case, <em>multiple</em> annotations (possibly parameterized) may be listed on the same line; 746for example:</p> 747 748<pre class="prettyprint lang-java">@Partial @Mock DataLoader loader; 749</pre> 750 751<p>There are no specific rules for formatting annotations on parameters, local variables, or types. 752</p> 753 754<a name="comments"></a> 755<h4 id="s4.8.6-comments">4.8.6 Comments</h4> 756 757<p>This section addresses <em>implementation comments</em>. Javadoc is addressed separately in 758Section 7, <a href="#s7-javadoc">Javadoc</a>.</p> 759 760<p>Any line break may be preceded by arbitrary whitespace followed by an implementation comment. 761Such a comment renders the line non-blank.</p> 762 763<h5 id="s4.8.6.1-block-comment-style">4.8.6.1 Block comment style</h5> 764 765<p>Block comments are indented at the same level as the surrounding code. They may be in 766<code class="prettyprint lang-java">/* ... */</code> style or 767<code class="prettyprint lang-java">// ...</code> style. For multi-line 768<code class="prettyprint lang-java">/* ... */</code> comments, subsequent lines must start with 769<code>*</code> aligned with the <code>*</code> on the previous line.</p> 770 771<pre class="prettyprint lang-java">/* 772 * This is // And so /* Or you can 773 * okay. // is this. * even do this. */ 774 */ 775</pre> 776 777 778<p>Comments are not enclosed in boxes drawn with asterisks or other characters.</p> 779 780<p class="tip"><strong>Tip:</strong> When writing multi-line comments, use the 781<code class="prettyprint lang-java">/* ... */</code> style if you want automatic code formatters to 782re-wrap the lines when necessary (paragraph-style). Most formatters don't re-wrap lines in 783<code class="prettyprint lang-java">// ...</code> style comment blocks.</p> 784 785 786 787<a name="modifiers"></a> 788<h4 id="s4.8.7-modifiers">4.8.7 Modifiers</h4> 789 790<p>Class and member modifiers, when present, appear in the order 791recommended by the Java Language Specification: 792</p> 793 794<pre>public protected private abstract default static final transient volatile synchronized native strictfp 795</pre> 796 797<h4 id="s4.8.8-numeric-literals">4.8.8 Numeric Literals</h4> 798 799<p><code>long</code>-valued integer literals use an uppercase <code>L</code> suffix, never 800lowercase (to avoid confusion with the digit <code>1</code>). For example, <code>3000000000L</code> 801rather than <code class="badcode">3000000000l</code>.</p> 802 803<a name="naming"></a> 804<h2 id="s5-naming">5 Naming</h2> 805 806<h3 id="s5.1-identifier-names">5.1 Rules common to all identifiers</h3> 807 808<p>Identifiers use only ASCII letters and digits, and, in a small number of cases noted below, 809underscores. Thus each valid identifier name is matched by the regular expression 810<code>\w+</code> .</p> 811 812<p>In Google Style special prefixes or 813suffixes, like those seen in the examples <code class="badcode">name_</code>, 814<code class="badcode">mName</code>, <code class="badcode">s_name</code> and 815<code class="badcode">kName</code>, are <strong>not</strong> used.</p> 816 817<h3 id="s5.2-specific-identifier-names">5.2 Rules by identifier type</h3> 818 819<h4 id="s5.2.1-package-names">5.2.1 Package names</h4> 820 821<p>Package names are all lowercase, with consecutive words simply concatenated together (no 822underscores). For example, <code>com.example.deepspace</code>, not 823<code class="badcode">com.example.deepSpace</code> or 824<code class="badcode">com.example.deep_space</code>.</p> 825 826<h4 id="s5.2.2-class-names">5.2.2 Class names</h4> 827 828<p>Class names are written in <a href="#s5.3-camel-case">UpperCamelCase</a>.</p> 829 830<p>Class names are typically nouns or noun phrases. For example, 831<code class="prettyprint lang-java">Character</code> or 832<code class="prettyprint lang-java">ImmutableList</code>. Interface names may also be nouns or 833noun phrases (for example, <code class="prettyprint lang-java">List</code>), but may sometimes be 834adjectives or adjective phrases instead (for example, 835<code class="prettyprint lang-java">Readable</code>).</p> 836 837<p>There are no specific rules or even well-established conventions for naming annotation types.</p> 838 839<p><em>Test</em> classes are named starting with the name of the class they are testing, and ending 840with <code class="prettyprint lang-java">Test</code>. For example, 841<code class="prettyprint lang-java">HashTest</code> or 842<code class="prettyprint lang-java">HashIntegrationTest</code>.</p> 843 844<h4 id="s5.2.3-method-names">5.2.3 Method names</h4> 845 846<p>Method names are written in <a href="#s5.3-camel-case">lowerCamelCase</a>.</p> 847 848<p>Method names are typically verbs or verb phrases. For example, 849<code class="prettyprint lang-java">sendMessage</code> or 850<code class="prettyprint lang-java">stop</code>.</p> 851 852<p>Underscores may appear in JUnit <em>test</em> method names to separate logical components of the 853name, with <em>each</em> component written in <a href="#s5.3-camel-case">lowerCamelCase</a>. 854One typical pattern is <code><i><methodUnderTest></i>_<i><state></i></code>, 855for example <code class="prettyprint lang-java">pop_emptyStack</code>. There is no One Correct 856Way to name test methods.</p> 857 858<a name="constants"></a> 859<h4 id="s5.2.4-constant-names">5.2.4 Constant names</h4> 860 861<p>Constant names use <code class="prettyprint lang-java">CONSTANT_CASE</code>: all uppercase 862letters, with each word separated from the next by a single underscore. But what <em>is</em> a 863constant, exactly?</p> 864 865<p>Constants are static final fields whose contents are deeply immutable and whose methods have no 866detectable side effects. This includes primitives, Strings, immutable types, and immutable 867collections of immutable types. If any of the instance's observable state can change, it is not a 868constant. Merely <em>intending</em> to never mutate the object is not enough. Examples:</p> 869 870<pre class="prettyprint lang-java">// Constants 871static final int NUMBER = 5; 872static final ImmutableList<String> NAMES = ImmutableList.of("Ed", "Ann"); 873static final ImmutableMap<String, Integer> AGES = ImmutableMap.of("Ed", 35, "Ann", 32); 874static final Joiner COMMA_JOINER = Joiner.on(','); // because Joiner is immutable 875static final SomeMutableType[] EMPTY_ARRAY = {}; 876enum SomeEnum { ENUM_CONSTANT } 877 878// Not constants 879static String nonFinal = "non-final"; 880final String nonStatic = "non-static"; 881static final Set<String> mutableCollection = new HashSet<String>(); 882static final ImmutableSet<SomeMutableType> mutableElements = ImmutableSet.of(mutable); 883static final ImmutableMap<String, SomeMutableType> mutableValues = 884 ImmutableMap.of("Ed", mutableInstance, "Ann", mutableInstance2); 885static final Logger logger = Logger.getLogger(MyClass.getName()); 886static final String[] nonEmptyArray = {"these", "can", "change"}; 887</pre> 888 889<p>These names are typically nouns or noun phrases.</p> 890 891<h4 id="s5.2.5-non-constant-field-names">5.2.5 Non-constant field names</h4> 892 893<p>Non-constant field names (static or otherwise) are written 894in <a href="#s5.3-camel-case">lowerCamelCase</a>.</p> 895 896<p>These names are typically nouns or noun phrases. For example, 897<code class="prettyprint lang-java">computedValues</code> or 898<code class="prettyprint lang-java">index</code>.</p> 899 900<h4 id="s5.2.6-parameter-names">5.2.6 Parameter names</h4> 901 902<p>Parameter names are written in <a href="#s5.3-camel-case">lowerCamelCase</a>.</p> 903 904<p>One-character parameter names in public methods should be avoided.</p> 905 906<h4 id="s5.2.7-local-variable-names">5.2.7 Local variable names</h4> 907 908<p>Local variable names are written in <a href="#s5.3-camel-case">lowerCamelCase</a>.</p> 909 910<p>Even when final and immutable, local variables are not considered to be constants, and should not 911be styled as constants.</p> 912 913<h4 id="s5.2.8-type-variable-names">5.2.8 Type variable names</h4> 914 915<p>Each type variable is named in one of two styles:</p> 916 917<ul> 918 <li>A single capital letter, optionally followed by a single numeral (such as 919 <code class="prettyprint lang-java">E</code>, <code class="prettyprint lang-java">T</code>, 920 <code class="prettyprint lang-java">X</code>, <code class="prettyprint lang-java">T2</code>) 921 </li> 922 923 <li>A name in the form used for classes (see Section 5.2.2, 924 <a href="#s5.2.2-class-names">Class names</a>), followed by the capital letter 925 <code class="prettyprint lang-java">T</code> (examples: 926 <code class="prettyprint lang-java">RequestT</code>, 927 <code class="prettyprint lang-java">FooBarT</code>).</li> 928</ul> 929 930<a name="acronyms"></a> 931<a name="camelcase"></a> 932<h3 id="s5.3-camel-case">5.3 Camel case: defined</h3> 933 934<p>Sometimes there is more than one reasonable way to convert an English phrase into camel case, 935such as when acronyms or unusual constructs like "IPv6" or "iOS" are present. To improve 936predictability, Google Style specifies the following (nearly) deterministic scheme.</p> 937 938<p>Beginning with the prose form of the name:</p> 939 940<ol> 941 <li>Convert the phrase to plain ASCII and remove any apostrophes. For example, "Müller's 942 algorithm" might become "Muellers algorithm".</li> 943 944 <li>Divide this result into words, splitting on spaces and any remaining punctuation (typically 945 hyphens). 946 947 <ul> 948 <li><em>Recommended:</em> if any word already has a conventional camel-case appearance in common 949 usage, split this into its constituent parts (e.g., "AdWords" becomes "ad words"). Note 950 that a word such as "iOS" is not really in camel case <em>per se</em>; it defies <em>any</em> 951 convention, so this recommendation does not apply.</li> 952 </ul> 953 </li> 954 955 <li>Now lowercase <em>everything</em> (including acronyms), then uppercase only the first 956 character of: 957 <ul> 958 <li>... each word, to yield <em>upper camel case</em>, or</li> 959 <li>... each word except the first, to yield <em>lower camel case</em></li> 960 </ul> 961 </li> 962 963 <li>Finally, join all the words into a single identifier.</li> 964</ol> 965 966<p>Note that the casing of the original words is almost entirely disregarded. Examples:</p> 967 968<table> 969 <tbody><tr> 970 <th>Prose form</th> 971 <th>Correct</th> 972 <th>Incorrect</th> 973 </tr> 974 <tr> 975 <td>"XML HTTP request"</td> 976 <td><code class="prettyprint lang-java">XmlHttpRequest</code></td> 977 <td><code class="badcode">XMLHTTPRequest</code></td> 978 </tr> 979 <tr> 980 <td>"new customer ID"</td> 981 <td><code class="prettyprint lang-java">newCustomerId</code></td> 982 <td><code class="badcode">newCustomerID</code></td> 983 </tr> 984 <tr> 985 <td>"inner stopwatch"</td> 986 <td><code class="prettyprint lang-java">innerStopwatch</code></td> 987 <td><code class="badcode">innerStopWatch</code></td> 988 </tr> 989 <tr> 990 <td>"supports IPv6 on iOS?"</td> 991 <td><code class="prettyprint lang-java">supportsIpv6OnIos</code></td> 992 <td><code class="badcode">supportsIPv6OnIOS</code></td> 993 </tr> 994 <tr> 995 <td>"YouTube importer"</td> 996 <td><code class="prettyprint lang-java">YouTubeImporter</code><br> 997 <code class="prettyprint lang-java">YoutubeImporter</code>*</td> 998 <td></td> 999 </tr> 1000</tbody></table> 1001 1002<p>*Acceptable, but not recommended.</p> 1003 1004<p class="note"><strong>Note:</strong> Some words are ambiguously hyphenated in the English 1005language: for example "nonempty" and "non-empty" are both correct, so the method names 1006<code class="prettyprint lang-java">checkNonempty</code> and 1007<code class="prettyprint lang-java">checkNonEmpty</code> are likewise both correct.</p> 1008 1009 1010<h2 id="s6-programming-practices">6 Programming Practices</h2> 1011 1012<h3 id="s6.1-override-annotation">6.1 <code>@Override</code>: always used</h3> 1013 1014<p>A method is marked with the <code class="prettyprint lang-java">@Override</code> annotation 1015whenever it is legal. This includes a class method overriding a superclass method, a class method 1016implementing an interface method, and an interface method respecifying a superinterface 1017method.</p> 1018 1019<p class="exception"><strong>Exception:</strong> 1020<code class="prettyprint lang-java">@Override</code> may be omitted when the parent method is 1021<code class="prettyprint lang-java">@Deprecated</code>.</p> 1022 1023<a name="caughtexceptions"></a> 1024<h3 id="s6.2-caught-exceptions">6.2 Caught exceptions: not ignored</h3> 1025 1026<p>Except as noted below, it is very rarely correct to do nothing in response to a caught 1027exception. (Typical responses are to log it, or if it is considered "impossible", rethrow it as an 1028<code class="prettyprint lang-java">AssertionError</code>.)</p> 1029 1030<p>When it truly is appropriate to take no action whatsoever in a catch block, the reason this is 1031justified is explained in a comment.</p> 1032 1033<pre class="prettyprint lang-java">try { 1034 int i = Integer.parseInt(response); 1035 return handleNumericResponse(i); 1036} catch (NumberFormatException ok) { 1037 // it's not numeric; that's fine, just continue 1038} 1039return handleTextResponse(response); 1040</pre> 1041 1042<p class="exception"><strong>Exception:</strong> In tests, a caught exception may be ignored 1043without comment <em>if</em> its name is or begins with <code class="prettyprint lang-java">expected</code>. The 1044following is a very common idiom for ensuring that the code under test <em>does</em> throw an 1045exception of the expected type, so a comment is unnecessary here.</p> 1046 1047<pre class="prettyprint lang-java">try { 1048 emptyStack.pop(); 1049 fail(); 1050} catch (NoSuchElementException expected) { 1051} 1052</pre> 1053 1054<h3 id="s6.3-static-members">6.3 Static members: qualified using class</h3> 1055 1056<p>When a reference to a static class member must be qualified, it is qualified with that class's 1057name, not with a reference or expression of that class's type.</p> 1058 1059<pre class="prettyprint lang-java">Foo aFoo = ...; 1060Foo.aStaticMethod(); // good 1061<span class="badcode">aFoo.aStaticMethod();</span> // bad 1062<span class="badcode">somethingThatYieldsAFoo().aStaticMethod();</span> // very bad 1063</pre> 1064 1065<a name="finalizers"></a> 1066<h3 id="s6.4-finalizers">6.4 Finalizers: not used</h3> 1067 1068<p>It is <strong>extremely rare</strong> to override <code class="prettyprint 1069lang-java">Object.finalize</code>.</p> 1070 1071<p class="tip"><strong>Tip:</strong> Don't do it. If you absolutely must, first read and understand 1072 1073 1074 <a href="http://books.google.com/books?isbn=8131726592"><em>Effective Java</em> Item 7,</a> 1075 1076"Avoid Finalizers," very carefully, and <em>then</em> don't do it.</p> 1077 1078 1079<a name="javadoc"></a> 1080<h2 id="s7-javadoc">7 Javadoc</h2> 1081 1082 1083 1084<h3 id="s7.1-javadoc-formatting">7.1 Formatting</h3> 1085 1086<h4 id="s7.1.1-javadoc-multi-line">7.1.1 General form</h4> 1087 1088<p>The <em>basic</em> formatting of Javadoc blocks is as seen in this example:</p> 1089 1090<pre class="prettyprint lang-java">/** 1091 * Multiple lines of Javadoc text are written here, 1092 * wrapped normally... 1093 */ 1094public int method(String p1) { ... } 1095</pre> 1096 1097<p>... or in this single-line example:</p> 1098 1099<pre class="prettyprint lang-java">/** An especially short bit of Javadoc. */ 1100</pre> 1101 1102<p>The basic form is always acceptable. The single-line form may be substituted when the entirety 1103of the Javadoc block (including comment markers) can fit on a single line. Note that this only 1104applies when there are no block tags such as <code>@return</code>. 1105 1106</p><h4 id="s7.1.2-javadoc-paragraphs">7.1.2 Paragraphs</h4> 1107 1108<p>One blank line—that is, a line containing only the aligned leading asterisk 1109(<code>*</code>)—appears between paragraphs, and before the group of block tags if 1110present. Each paragraph but the first has <code><p></code> immediately before the first word, 1111with no space after.</p> 1112 1113<a name="s7.1.3-javadoc-at-clauses"></a> 1114 1115<h4 id="s7.1.3-javadoc-block-tags">7.1.3 Block tags</h4> 1116 1117<p>Any of the standard "block tags" that are used appear in the order <code>@param</code>, 1118<code>@return</code>, <code>@throws</code>, <code>@deprecated</code>, and these four types never 1119appear with an empty description. When a block tag doesn't fit on a single line, continuation lines 1120are indented four (or more) spaces from the position of the <code>@</code>. 1121</p> 1122 1123<h3 id="s7.2-summary-fragment">7.2 The summary fragment</h3> 1124 1125<p>Each Javadoc block begins with a brief <strong>summary fragment</strong>. This 1126fragment is very important: it is the only part of the text that appears in certain contexts such as 1127class and method indexes.</p> 1128 1129<p>This is a fragment—a noun phrase or verb phrase, not a complete sentence. It does 1130<strong>not</strong> begin with <code class="badcode">A {@code Foo} is a...</code>, or 1131<code class="badcode">This method returns...</code>, nor does it form a complete imperative sentence 1132like <code class="badcode">Save the record.</code>. However, the fragment is capitalized and 1133punctuated as if it were a complete sentence.</p> 1134 1135<p class="tip"><strong>Tip:</strong> A common mistake is to write simple Javadoc in the form 1136<code class="badcode">/** @return the customer ID */</code>. This is incorrect, and should be 1137changed to <code class="prettyprint lang-java">/** Returns the customer ID. */</code>.</p> 1138 1139<a name="s7.3.3-javadoc-optional"></a> 1140<h3 id="s7.3-javadoc-where-required">7.3 Where Javadoc is used</h3> 1141 1142<p>At the <em>minimum</em>, Javadoc is present for every 1143<code class="prettyprint lang-java">public</code> class, and every 1144<code class="prettyprint lang-java">public</code> or 1145<code class="prettyprint lang-java">protected</code> member of such a class, with a few exceptions 1146noted below.</p> 1147 1148<p>Additional Javadoc content may also be present, as explained in Section 7.3.4, 1149<a href="#s7.3.4-javadoc-non-required">Non-required Javadoc</a>.</p> 1150 1151<h4 id="s7.3.1-javadoc-exception-self-explanatory">7.3.1 Exception: self-explanatory methods</h4> 1152 1153<p>Javadoc is optional for "simple, obvious" methods like 1154<code class="prettyprint lang-java">getFoo</code>, in cases where there <em>really and truly</em> is 1155nothing else worthwhile to say but "Returns the foo".</p> 1156 1157<p class="note"><strong>Important:</strong> it is not appropriate to cite this exception to justify 1158omitting relevant information that a typical reader might need to know. For example, for a method 1159named <code class="prettyprint lang-java">getCanonicalName</code>, don't omit its documentation 1160(with the rationale that it would say only 1161<code class="badcode">/** Returns the canonical name. */</code>) if a typical reader may have no idea 1162what the term "canonical name" means!</p> 1163 1164<h4 id="s7.3.2-javadoc-exception-overrides">7.3.2 Exception: overrides</h4> 1165 1166<p>Javadoc is not always present on a method that overrides a supertype method. 1167 1168</p> 1169 1170 1171 1172<h4 id="s7.3.4-javadoc-non-required">7.3.4 Non-required Javadoc</h4> 1173 1174<p>Other classes and members have Javadoc <em>as needed or desired</em>. 1175 1176</p><p>Whenever an implementation comment would be used to define the overall purpose or behavior of a 1177class or member, that comment is written as Javadoc instead (using <code>/**</code>).</p> 1178 1179<p>Non-required Javadoc is not strictly required to follow the formatting rules of Sections 11807.1.2, 7.1.3, and 7.2, though it is of course recommended.</p> 1181 1182 1183 1184</div> 1185</div> 1186</body> 1187</html> 1188