Skip to content

Commit 1387bff

Browse files
eamonnmcmanusvapier
authored andcommitted
Update Java Style Guide following discussions.
The edits here are the result of discussions with Kevin Bourrillion, the original author of much of this guide.
1 parent c6f57a9 commit 1387bff

File tree

1 file changed

+52
-54
lines changed

1 file changed

+52
-54
lines changed

javaguide.html

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ <h1>Google Java Style Guide</h1>
1414
<div id="tocDiv" class="vertical_toc"></div>
1515

1616
<div class="main_body">
17-
1817
<h2 id="s1-introduction">1 Introduction</h2>
1918

2019
<p>This document serves as the <strong>complete</strong> definition of Google's coding standards for
@@ -38,12 +37,11 @@ <h3 id="s1.1-terminology">1.1 Terminology notes</h3>
3837
<p>In this document, unless otherwise clarified:</p>
3938

4039
<ol>
41-
<li>The term <em>class</em> is used inclusively to mean an "ordinary" class, record class, enum
40+
<li>The term <em>class</em> is used inclusively to mean a normal class, record class, enum
4241
class, interface or annotation type (<code class="prettyprint lang-java">@interface</code>).</li>
4342

4443
<li>The term <em>member</em> (of a class) is used inclusively to mean a nested class, field,
45-
method, <em>or constructor</em>; that is, all top-level contents of a class except initializers
46-
and comments.
44+
method, <em>or constructor</em>; that is, all top-level contents of a class except initializers.
4745

4846
</li><li>The term <em>comment</em> always refers to <em>implementation</em> comments. We do not
4947
use the phrase "documentation comments", and instead use the common term "Javadoc."</li>
@@ -79,7 +77,8 @@ <h4 id="s2.3.1-whitespace-characters">2.3.1 Whitespace characters</h4>
7977
anywhere in a source file. This implies that:</p>
8078

8179
<ol>
82-
<li>All other whitespace characters in string and character literals are escaped.</li>
80+
<li>All other whitespace characters are escaped in <code>char</code> and string literals and in
81+
text blocks.</li>
8382

8483
<li>Tab characters are <strong>not</strong> used for indentation.</li>
8584
</ol>
@@ -158,61 +157,62 @@ <h4 id="s2.3.3-non-ascii-characters">2.3.3 Non-ASCII characters</h4>
158157
<h2 id="s3-source-file-structure">3 Source file structure</h2>
159158

160159
<div>
161-
<p>An ordinary source file consists of, <strong>in order</strong>:</p>
160+
<p>An ordinary source file consists of these sections, <strong>in order</strong>:</p>
162161

163162
<ol>
164163
<li>License or copyright information, if present</li>
165-
<li>Package statement</li>
166-
<li>Import statements</li>
167-
<li>Exactly one top-level class</li>
164+
<li>Package declaration</li>
165+
<li>Imports</li>
166+
<li>Exactly one top-level class declaration</li>
168167
</ol>
169168
</div>
170169

171170
<p><strong>Exactly one blank line</strong> separates each section that is present.</p>
172171

173-
<p>A <code>package-info.java</code> file is the same, but without the top-level class.</p>
172+
<p>A <code>package-info.java</code> file is the same, but without the class declaration.</p>
174173

175-
<p>A <code>module-info.java</code> file does not contain a package statement and replaces the
176-
single top-level class with a module declaration, but otherwise follows the same structure.</p>
174+
<p>A <code>module-info.java</code> file does not contain a package declaration and replaces the
175+
class declaration with a module declaration, but otherwise follows the same structure.</p>
177176

178177
<h3 id="s3.1-copyright-statement">3.1 License or copyright information, if present</h3>
179178

180179
<p>If license or copyright information belongs in a file, it belongs here.</p>
181180

182181

183182

184-
<h3 id="s3.2-package-statement">3.2 Package statement</h3>
183+
<a id="s3.2-package-statement"></a>
184+
<h3 id="s3.2-package-declaration">3.2 Package declaration</h3>
185185

186-
<p>The package statement is <strong>not line-wrapped</strong>. The column limit (Section 4.4,
187-
<a href="#s4.4-column-limit">Column limit: 100</a>) does not apply to package statements.</p>
186+
<p>The package declaration is <strong>not line-wrapped</strong>. The column limit (Section 4.4,
187+
<a href="#s4.4-column-limit">Column limit: 100</a>) does not apply to package declarations.</p>
188188

189189
<a id="imports"></a>
190-
<h3 id="s3.3-import-statements">3.3 Import statements</h3>
190+
<h3 id="s3.3-import-statements">3.3 Imports</h3>
191191

192192
<h4 id="s3.3.1-wildcard-imports">3.3.1 No wildcard imports</h4>
193193

194-
<p><strong>Wildcard imports</strong>, static or otherwise, <strong>are not used</strong>.</p>
194+
<p><strong>Wildcard ("on-demand") imports</strong>, static or otherwise, <strong>are not
195+
used</strong>.</p>
195196

196197
<h4 id="s3.3.2-import-line-wrapping">3.3.2 No line-wrapping</h4>
197198

198-
<p>Import statements are <strong>not line-wrapped</strong>. The column limit (Section 4.4,
199-
<a href="#s4.4-column-limit">Column limit: 100</a>) does not apply to import
200-
statements.</p>
199+
<p>Imports are <strong>not line-wrapped</strong>. The column limit (Section 4.4,
200+
<a href="#s4.4-column-limit">Column limit: 100</a>) does not apply to imports.</p>
201201

202202
<h4 id="s3.3.3-import-ordering-and-spacing">3.3.3 Ordering and spacing</h4>
203203

204204
<p>Imports are ordered as follows:</p>
205205

206206
<ol>
207-
<li>All static imports in a single block.</li>
208-
<li>All non-static imports in a single block.</li>
207+
<li>All static imports in a single group.</li>
208+
<li>All non-static imports in a single group.</li>
209209
</ol>
210210

211211
<p>If there are both static and non-static imports, a single blank line separates the two
212-
blocks. There are no other blank lines between import statements.</p>
212+
groups. There are no other blank lines between imports.</p>
213213

214-
<p>Within each block the imported names appear in ASCII sort order. (<strong>Note:</strong>
215-
this is not the same as the import <em>statements</em> being in ASCII sort order, since '.'
214+
<p>Within each group the imported names appear in ASCII sort order. (<strong>Note:</strong>
215+
this is not the same as the import <em>lines</em> being in ASCII sort order, since '.'
216216
sorts before ';'.)</p>
217217

218218

@@ -247,9 +247,9 @@ <h4 id="s3.4.2-ordering-class-contents">3.4.2 Ordering of class contents</h4>
247247
<h5 id="s3.4.2.1-overloads-never-split">3.4.2.1 Overloads: never split</h5>
248248

249249
<p>Methods of a class that share the same name appear in a single contiguous group with no other
250-
members in between. The same applies to multiple constructors (which always have the same name).
251-
This rule applies even when modifiers such as <code class="prettyprint lang-java">static</code> or
252-
<code class="prettyprint lang-java">private</code> differ between the methods.</p>
250+
members in between. The same applies to multiple constructors. This rule applies even when
251+
modifiers such as <code class="prettyprint lang-java">static</code> or
252+
<code class="prettyprint lang-java">private</code> differ between the methods or constructors.</p>
253253

254254
<h3 id="s3.5-module-declaration">3.5 Module declaration</h3>
255255

@@ -270,7 +270,7 @@ <h4 id="s3.5.1-ordering-module-directives">3.5.1 Ordering and spacing of module
270270
<h2 id="s4-formatting">4 Formatting</h2>
271271

272272
<p class="terminology"><strong>Terminology Note:</strong> <em>block-like construct</em> refers to
273-
the body of a class, method or constructor. Note that, by Section 4.8.3.1 on
273+
the body of a class, method, constructor, or switch. Note that, by Section 4.8.3.1 on
274274
<a href="#s4.8.3.1-array-initializers">array initializers</a>, any array initializer
275275
<em>may</em> optionally be treated as if it were a block-like construct.</p>
276276

@@ -291,9 +291,8 @@ <h4 id="s4.1.1-braces-always-used">4.1.1 Use of optional braces</h4>
291291

292292
<h4 id="s4.1.2-blocks-k-r-style">4.1.2 Nonempty blocks: K &amp; R style</h4>
293293

294-
<p>Braces follow the Kernighan and Ritchie style
295-
("<a href="https://blog.codinghorror.com/new-programming-jargon/#3">Egyptian brackets</a>")
296-
for <em>nonempty</em> blocks and block-like constructs:</p>
294+
<p>Braces follow the Kernighan and Ritchie style for <em>nonempty</em> blocks and block-like
295+
constructs:</p>
297296

298297
<ul>
299298
<li>No line break before the opening brace, except as detailed below.</li>
@@ -401,12 +400,11 @@ <h3 id="s4.4-column-limit">4.4 Column limit: 100</h3>
401400
<li>Lines where obeying the column limit is not possible (for example, a long URL in Javadoc,
402401
or a long JSNI method reference).</li>
403402

404-
<li><code class="prettyprint lang-java">package</code> and
405-
<code class="prettyprint lang-java">import</code> statements (see Sections
406-
3.2 <a href="#s3.2-package-statement">Package statement</a> and
407-
3.3 <a href="#s3.3-import-statements">Import statements</a>).</li>
403+
<li><code class="prettyprint lang-java">package</code> declarations and
404+
imports (see Sections 3.2 <a href="#s3.2-package-statement">Package declarations</a> and
405+
3.3 <a href="#s3.3-import-statements">Imports</a>).</li>
408406

409-
<li>Contents of <a href="s4.8.9-text-blocks">text blocks</a>.</li>
407+
<li>Contents of <a href="#s4.8.9-text-blocks">text blocks</a>.</li>
410408

411409
<li>Command lines in a comment that may be copied-and-pasted into a shell.</li>
412410

@@ -420,7 +418,7 @@ <h3 id="s4.4-column-limit">4.4 Column limit: 100</h3>
420418

421419
<h3 id="s4.5-line-wrapping">4.5 Line-wrapping</h3>
422420

423-
<p class="terminology"><strong>Terminology Note:</strong> When code that might otherwise legally
421+
<p class="terminology"><strong>Terminology Note:</strong> When code that might otherwise
424422
occupy a single line is divided into multiple lines, this activity is called
425423
<em>line-wrapping</em>.</p>
426424

@@ -461,7 +459,7 @@ <h4 id="s4.5.1-line-wrapping-where-to-break">4.5.1 Where to break</h4>
461459
<li>When a line is broken at an <em>assignment</em> operator the break typically comes
462460
<em>after</em> the symbol, but either way is acceptable.
463461
<ul>
464-
<li>This also applies to the "assignment-operator-like" colon in an enhanced
462+
<li>This also applies to the colon in an enhanced
465463
<code class="prettyprint lang-java">for</code> ("foreach") statement.</li>
466464
</ul>
467465
</li>
@@ -511,7 +509,7 @@ <h4 id="s4.5.2-line-wrapping-indent">4.5.2 Indent continuation lines at least +4
511509

512510
<h3 id="s4.6-whitespace">4.6 Whitespace</h3>
513511

514-
<h4 id="s4.6.1-vertical-whitespace">4.6.1 Vertical Whitespace</h4>
512+
<h4 id="s4.6.1-vertical-whitespace">4.6.1 Vertical whitespace (blank lines)</h4>
515513

516514
<p>A single blank line always appears:</p>
517515

@@ -529,7 +527,7 @@ <h4 id="s4.6.1-vertical-whitespace">4.6.1 Vertical Whitespace</h4>
529527

530528
<li>As required by other sections of this document (such as Section 3,
531529
<a href="#s3-source-file-structure">Source file structure</a>, and Section 3.3,
532-
<a href="#s3.3-import-statements">Import statements</a>).</li>
530+
<a href="#s3.3-import-statements">Imports</a>).</li>
533531
</ol>
534532

535533
<p>A single blank line may also appear anywhere it improves readability, for example between
@@ -541,18 +539,19 @@ <h4 id="s4.6.1-vertical-whitespace">4.6.1 Vertical Whitespace</h4>
541539

542540
<h4 id="s4.6.2-horizontal-whitespace">4.6.2 Horizontal whitespace</h4>
543541

544-
<p>Beyond where required by the language or other style rules, and apart from literals, comments and
545-
Javadoc, a single ASCII space also appears in the following places <strong>only</strong>.</p>
542+
<p>Beyond where required by the language or other style rules, and apart from within literals,
543+
comments and Javadoc, a single ASCII space also appears in the following places
544+
<strong>only</strong>.</p>
546545

547546
<ol>
548-
<li>Separating any reserved word, such as
547+
<li>Separating any keyword, such as
549548
<code class="prettyprint lang-java">if</code>,
550549
<code class="prettyprint lang-java">for</code> or
551550
<code class="prettyprint lang-java">catch</code>, from an open parenthesis
552551
(<code class="prettyprint lang-java">(</code>)
553552
that follows it on that line</li>
554553

555-
<li>Separating any reserved word, such as
554+
<li>Separating any keyword, such as
556555
<code class="prettyprint lang-java">else</code> or
557556
<code class="prettyprint lang-java">catch</code>, from a closing curly brace
558557
(<code class="prettyprint lang-java">}</code>) that precedes it on that line</li>
@@ -563,14 +562,14 @@ <h4 id="s4.6.2-horizontal-whitespace">4.6.2 Horizontal whitespace</h4>
563562
<li><code class="prettyprint lang-java">@SomeAnnotation({a, b})</code> (no space is used)</li>
564563

565564
<li><code class="prettyprint lang-java">String[][] x = {{"foo"}};</code> (no space is required
566-
between <code class="prettyprint lang-java">{{</code>, by item 9 below)</li>
565+
between <code class="prettyprint lang-java">{{</code>, by item 10 below)</li>
567566
</ul>
568567
</li>
569568

570569
<li>On both sides of any binary or ternary operator. This also applies to the following
571570
"operator-like" symbols:
572571
<ul>
573-
<li>the ampersand in a conjunctive type bound:
572+
<li>the ampersand that separates multiple type bounds:
574573
<code class="prettyprint lang-java">&lt;T extends Foo &amp; Bar&gt;</code></li>
575574

576575
<li>the pipe for a catch block that handles multiple exceptions:
@@ -603,7 +602,7 @@ <h4 id="s4.6.2-horizontal-whitespace">4.6.2 Horizontal whitespace</h4>
603602
<li>Between a double slash (<code class="prettyprint lang-java">//</code>) which begins a comment
604603
and the comment's text. Multiple spaces are allowed.</li>
605604

606-
<li>Between the type and variable of a declaration:
605+
<li>Between the type and identifier of a declaration:
607606
<code class="prettyprint lang-java">List&lt;String&gt; list</code></li>
608607

609608
<li><em>Optional</em> just inside both braces of an array initializer
@@ -638,13 +637,12 @@ <h4 id="s4.6.3-horizontal-alignment">4.6.3 Horizontal alignment: never required<
638637
private Color color; // may leave it unaligned
639638
</pre>
640639

641-
<p class="tip"><strong>Tip:</strong> Alignment can aid readability, but attempts to preserve
642-
alignment for its own sake create future problems. For example, consider a change that touches only
640+
<p class="tip"><strong>Tip:</strong> Alignment can aid readability, but attempting to preserve
641+
alignment for its own sake creates future problems. For example, consider a change that touches only
643642
one line. If that change disrupts the previous alignment, it's important **not** to introduce
644643
additional changes on nearby lines simply to realign them. Introducing formatting changes on
645644
otherwise unaffected lines corrupts version history, slows down reviewers, and exacerbates merge
646-
conflicts. These practical concerns
647-
take priority over alignment.</p>
645+
conflicts. These practical concerns take priority over alignment.</p>
648646

649647
<a id="parentheses"></a>
650648
<h3 id="s4.7-grouping-parentheses">4.7 Grouping parentheses: recommended</h3>
@@ -658,7 +656,7 @@ <h3 id="s4.8-specific-constructs">4.8 Specific constructs</h3>
658656

659657
<h4 id="s4.8.1-enum-classes">4.8.1 Enum classes</h4>
660658

661-
<p>After each comma that follows an enum constant, a line break is optional. Additional blank
659+
<p>After the comma that follows an enum constant, a line break is optional. Additional blank
662660
lines (usually just one) are also allowed. This is one possibility:
663661

664662
</p><pre class="prettyprint lang-java">private enum Answer {
@@ -770,7 +768,7 @@ <h5 id="s4.8.4.1-switch-indentation">4.8.4.1 Indentation</h5>
770768
</pre>
771769

772770
<p>In an old-style switch, the colon of each switch label is followed by a line break. The
773-
statements within a statement group start with a further +2 indentation.</p>
771+
statements within a statement group are indented a further +2.</p>
774772

775773
<a id="fallthrough"></a>
776774
<h5 id="s4.8.4.2-switch-fall-through">4.8.4.2 Fall-through: commented</h5>

0 commit comments

Comments
 (0)