@@ -42,7 +42,7 @@ <h3 id="s1.1-terminology">1.1 Terminology notes</h3>
42
42
and comments.
43
43
44
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 >
45
+ use the phrase "documentation comments", and instead use the common term "Javadoc."</ li >
46
46
</ ol >
47
47
48
48
< p > Other "terminology notes" will appear occasionally throughout the document.</ p >
@@ -236,8 +236,10 @@ <h4 id="s3.4.2-ordering-class-contents">3.4.2 Ordering of class contents</h4>
236
236
< a name ="overloads "> </ a >
237
237
< h5 id ="s3.4.2.1-overloads-never-split "> 3.4.2.1 Overloads: never split</ h5 >
238
238
239
- < p > When a class has multiple constructors, or multiple methods with the same name, these appear
240
- sequentially, with no other code in between (not even private members).</ p >
239
+ < p > Methods of a class that share the same name appear in a single contiguous group with no other
240
+ members in between. The same applies to multiple constructors (which always have the same name).
241
+ This rule applies even when modifiers such as < code class ="prettyprint lang-java "> static</ code > or
242
+ < code class ="prettyprint lang-java "> private</ code > differ between the methods.</ p >
241
243
242
244
< h2 id ="s4-formatting "> 4 Formatting</ h2 >
243
245
@@ -249,7 +251,7 @@ <h2 id="s4-formatting">4 Formatting</h2>
249
251
< a name ="braces "> </ a >
250
252
< h3 id ="s4.1-braces "> 4.1 Braces</ h3 >
251
253
252
- < h4 id ="s4.1.1-braces-always-used "> 4.1.1 Braces are used where optional</ h4 >
254
+ < h4 id ="s4.1.1-braces-always-used "> 4.1.1 Use of optional braces </ h4 >
253
255
254
256
< p > Braces are used with
255
257
< code class ="prettyprint lang-java "> if</ code > ,
@@ -259,14 +261,16 @@ <h4 id="s4.1.1-braces-always-used">4.1.1 Braces are used where optional</h4>
259
261
< code class ="prettyprint lang-java "> while</ code > statements, even when the
260
262
body is empty or contains only a single statement.</ p >
261
263
264
+ < p > Other optional braces, such as those in a lambda expression, remain optional.</ p >
265
+
262
266
< h4 id ="s4.1.2-blocks-k-r-style "> 4.1.2 Nonempty blocks: K & R style</ h4 >
263
267
264
268
< p > Braces follow the Kernighan and Ritchie style
265
269
("< a href ="http://www.codinghorror.com/blog/2012/07/new-programming-jargon.html "> Egyptian brackets</ a > ")
266
270
for < em > nonempty</ em > blocks and block-like constructs:</ p >
267
271
268
272
< ul >
269
- < li > No line break before the opening brace.</ li >
273
+ < li > No line break before the opening brace, except as detailed below .</ li >
270
274
271
275
< li > Line break after the opening brace.</ li >
272
276
@@ -278,6 +282,11 @@ <h4 id="s4.1.2-blocks-k-r-style">4.1.2 Nonempty blocks: K & R style</h4>
278
282
< code class ="prettyprint lang-java "> else</ code > or a comma.</ li >
279
283
</ ul >
280
284
285
+ < p > Exception: In places where these rules allow a single statement ending with a semicolon
286
+ (< code class ="prettyprint lang-java "> ;</ code > ), a block of statements can appear, and the opening
287
+ brace of this block is preceded by a line break. Blocks like these are typically introduced to
288
+ limit the scope of local variables, for example inside switch statements.</ p >
289
+
281
290
< p > Examples:</ p >
282
291
283
292
< pre class ="prettyprint lang-java "> return () -> {
@@ -299,6 +308,10 @@ <h4 id="s4.1.2-blocks-k-r-style">4.1.2 Nonempty blocks: K & R style</h4>
299
308
} else {
300
309
lastThing();
301
310
}
311
+ {
312
+ int x = foo();
313
+ frob(x);
314
+ }
302
315
}
303
316
};
304
317
</ pre >
@@ -367,7 +380,14 @@ <h3 id="s4.4-column-limit">4.4 Column limit: 100</h3>
367
380
3.2 < a href ="#s3.2-package-statement "> Package statement</ a > and
368
381
3.3 < a href ="#s3.3-import-statements "> Import statements</ a > ).</ li >
369
382
370
- < li > Command lines in a comment that may be cut-and-pasted into a shell.</ li >
383
+ < li > Command lines in a comment that may be copied-and-pasted into a shell.</ li >
384
+
385
+ < li > Very long identifiers, on the rare occasions they are called for, are allowed to exceed the
386
+ column limit. In that case, the valid wrapping for the surrounding code is as produced by
387
+
388
+
389
+ < a href ="https://github.com/google/google-java-format "> google-java-format</ a > .
390
+ </ li >
371
391
</ ol >
372
392
373
393
< h3 id ="s4.5-line-wrapping "> 4.5 Line-wrapping</ h3 >
@@ -509,7 +529,7 @@ <h4 id="s4.6.2-horizontal-whitespace">4.6.2 Horizontal whitespace</h4>
509
529
< li > < code class ="prettyprint lang-java "> @SomeAnnotation({a, b})</ code > (no space is used)</ li >
510
530
511
531
< 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 >
532
+ between < code class ="prettyprint lang-java "> {{</ code > , by item 9 below)</ li >
513
533
</ ul >
514
534
</ li >
515
535
@@ -541,8 +561,11 @@ <h4 id="s4.6.2-horizontal-whitespace">4.6.2 Horizontal whitespace</h4>
541
561
< li > After < code class ="prettyprint lang-java "> ,:;</ code > or the closing parenthesis
542
562
(< code class ="prettyprint lang-java "> )</ code > ) of a cast</ li >
543
563
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 >
564
+ < li > Between any content and a double slash (< code class ="prettyprint lang-java "> //</ code > ) which
565
+ begins a comment. Multiple spaces are allowed.</ li >
566
+
567
+ < li > Between a double slash (< code class ="prettyprint lang-java "> //</ code > ) which begins a comment
568
+ and the comment's text. Multiple spaces are allowed.</ li >
546
569
547
570
< li > Between the type and variable of a declaration:
548
571
< code class ="prettyprint lang-java "> List<String> list</ code > </ li >
@@ -712,7 +735,7 @@ <h5 id="s4.8.4.2-switch-fall-through">4.8.4.2 Fall-through: commented</h5>
712
735
< p > Notice that no comment is needed after < code class ="prettyprint lang-java "> case 1:</ code > , only
713
736
at the end of the statement group.</ p >
714
737
715
- < h5 id ="s4.8.4.3-switch-default "> 4.8.4.3 The < code > default</ code > case is present </ h5 >
738
+ < h5 id ="s4.8.4.3-switch-default "> 4.8.4.3 Presence of the < code > default</ code > label </ h5 >
716
739
717
740
< p > Each switch statement includes a < code class ="prettyprint lang-java "> default</ code > statement
718
741
group, even if it contains no code.</ p >
@@ -727,14 +750,37 @@ <h5 id="s4.8.4.3-switch-default">4.8.4.3 The <code>default</code> case is presen
727
750
< a name ="annotations "> </ a >
728
751
< h4 id ="s4.8.5-annotations "> 4.8.5 Annotations</ h4 >
729
752
730
- < p > Annotations applying to a class, method or constructor appear immediately after the
753
+ < h5 id ="s4.8.5.1-type-use-annotation-style "> 4.8.5.1 Type-use annotations</ h5 >
754
+
755
+ < p > Type-use annotations appear immediately before the annotated type. An annotation is a type-use
756
+ annotation if it is meta-annotated with
757
+ < code class ="prettyprint lang-java "> @Target(ElementType.TYPE_USE)</ code > . Example:</ p >
758
+
759
+ < pre class ="prettyprint lang-java "> final @Nullable String name;
760
+
761
+ public @Nullable Person getPersonByName(String name);
762
+ </ pre >
763
+
764
+ < h5 id ="s4.8.5.2-class-annotation-style "> 4.8.5.2 Class annotations</ h5 >
765
+
766
+ < p > Annotations applying to a class appear immediately after the
731
767
documentation block, and each annotation is listed on a line of its own (that is, one annotation
732
768
per line). These line breaks do not constitute line-wrapping (Section
733
769
4.5, < a href ="#s4.5-line-wrapping "> Line-wrapping</ a > ), so the indentation level is not
734
770
increased. Example:</ p >
735
771
736
- < pre class ="prettyprint lang-java "> @Override
737
- @Nullable
772
+ < pre class ="prettyprint lang-java "> @Deprecated
773
+ @CheckReturnValue
774
+ public final class Frozzler { ... }
775
+ </ pre >
776
+
777
+ < h5 id ="s4.8.5.3-method-annotation-style "> 4.8.5.3 Method and constructor annotations</ h5 >
778
+
779
+ < p > The rules for annotations on method and constructor declarations are the same as the
780
+ < a href ="#s4.8.5.2-class-annotation-style "> previous section</ a > . Example: </ p >
781
+
782
+ < pre class ="prettyprint lang-java "> @Deprecated
783
+ @Override
738
784
public String getNameIfPresent() { ... }
739
785
</ pre >
740
786
@@ -744,15 +790,19 @@ <h4 id="s4.8.5-annotations">4.8.5 Annotations</h4>
744
790
< pre class ="prettyprint lang-java "> @Override public int hashCode() { ... }
745
791
</ pre >
746
792
793
+ < h5 id ="s4.8.5.4-field-annotation-style "> 4.8.5.4 Field annotations</ h5 >
794
+
747
795
< p > Annotations applying to a field also appear immediately after the documentation block, but in
748
796
this case, < em > multiple</ em > annotations (possibly parameterized) may be listed on the same line;
749
797
for example:</ p >
750
798
751
799
< pre class ="prettyprint lang-java "> @Partial @Mock DataLoader loader;
752
800
</ pre >
753
801
754
- < p > There are no specific rules for formatting annotations on parameters, local variables, or types.
755
- </ p >
802
+ < h5 id ="s4.8.5.5-local-parameter-annotation-style "> 4.8.5.5 Parameter and local variable annotations</ h5 >
803
+
804
+ < p > There are no specific rules for formatting annotations on parameters or local variables (except,
805
+ of course, when the annotation is a type-use annotation).</ p >
756
806
757
807
< a name ="comments "> </ a >
758
808
< h4 id ="s4.8.6-comments "> 4.8.6 Comments</ h4 >
@@ -820,8 +870,8 @@ <h3 id="s5.2-specific-identifier-names">5.2 Rules by identifier type</h3>
820
870
821
871
< h4 id ="s5.2.1-package-names "> 5.2.1 Package names</ h4 >
822
872
823
- < p > Package names are all lowercase, with consecutive words simply concatenated together (no
824
- underscores) . For example, < code > com.example.deepspace</ code > , not
873
+ < p > Package names use only lowercase letters and digits (no underscores). Consecutive words are
874
+ simply concatenated together . For example, < code > com.example.deepspace</ code > , not
825
875
< code class ="badcode "> com.example.deepSpace</ code > or
826
876
< code class ="badcode "> com.example.deep_space</ code > .</ p >
827
877
@@ -838,10 +888,11 @@ <h4 id="s5.2.2-class-names">5.2.2 Class names</h4>
838
888
839
889
< p > There are no specific rules or even well-established conventions for naming annotation types.</ p >
840
890
841
- < p > < em > Test</ em > classes are named starting with the name of the class they are testing, and ending
842
- with < code class ="prettyprint lang-java "> Test</ code > . For example,
843
- < code class ="prettyprint lang-java "> HashTest</ code > or
844
- < code class ="prettyprint lang-java "> HashIntegrationTest</ code > .</ p >
891
+ < p > A < em > test</ em > class has a name that ends with < code class ="prettyprint lang-java "> Test</ code > ,
892
+ for example, < code class ="prettyprint lang-java "> HashIntegrationTest</ code > .
893
+ If it covers a single class, its name is the name of that class
894
+ plus < code class ="prettyprint lang-java "> Test</ code > , for example < code class ="prettyprint
895
+ lang-java "> HashImplTest</ code > .</ p >
845
896
846
897
< h4 id ="s5.2.3-method-names "> 5.2.3 Method names</ h4 >
847
898
@@ -852,30 +903,28 @@ <h4 id="s5.2.3-method-names">5.2.3 Method names</h4>
852
903
< code class ="prettyprint lang-java "> stop</ code > .</ p >
853
904
854
905
< p > Underscores may appear in JUnit < em > test</ em > method names to separate logical components of the
855
- name, with < em > each</ em > component written in < a href ="#s5.3-camel-case "> lowerCamelCase</ a > .
856
- One typical pattern is < code > < i > <methodUnderTest></ i > _< i > <state></ i > </ code > ,
857
- for example < code class ="prettyprint lang-java "> pop_emptyStack</ code > . There is no One Correct
858
- Way to name test methods.</ p >
906
+ name, with < em > each</ em > component written in < a href ="#s5.3-camel-case "> lowerCamelCase</ a > , for
907
+ example < code class ="prettyprint lang-java "> transferMoney_deductsFromSource</ code > . There is no One
908
+ Correct Way to name test methods.</ p >
859
909
860
910
< a name ="constants "> </ a >
861
911
< h4 id ="s5.2.4-constant-names "> 5.2.4 Constant names</ h4 >
862
912
863
- < p > Constant names use < code class ="prettyprint lang-java "> CONSTANT_CASE </ code > : all uppercase
913
+ < p > Constant names use < code class ="prettyprint lang-java "> UPPER_SNAKE_CASE </ code > : all uppercase
864
914
letters, with each word separated from the next by a single underscore. But what < em > is</ em > a
865
915
constant, exactly?</ p >
866
916
867
917
< p > Constants are static final fields whose contents are deeply immutable and whose methods have no
868
- detectable side effects. This includes primitives, Strings , immutable types , and immutable
869
- collections of immutable types . If any of the instance's observable state can change, it is not a
918
+ detectable side effects. Examples include primitives, strings , immutable value classes , and anything
919
+ set to < code > null </ code > . If any of the instance's observable state can change, it is not a
870
920
constant. Merely < em > intending</ em > to never mutate the object is not enough. Examples:</ p >
871
921
872
922
< pre class ="prettyprint lang-java "> // Constants
873
923
static final int NUMBER = 5;
874
924
static final ImmutableList<String> NAMES = ImmutableList.of("Ed", "Ann");
875
- static final ImmutableMap <String, Integer> AGES = ImmutableMap.of("Ed", 35, "Ann", 32);
925
+ static final Map <String, Integer> AGES = ImmutableMap.of("Ed", 35, "Ann", 32);
876
926
static final Joiner COMMA_JOINER = Joiner.on(','); // because Joiner is immutable
877
927
static final SomeMutableType[] EMPTY_ARRAY = {};
878
- enum SomeEnum { ENUM_CONSTANT }
879
928
880
929
// Not constants
881
930
static String nonFinal = "non-final";
@@ -1073,9 +1122,9 @@ <h3 id="s6.4-finalizers">6.4 Finalizers: not used</h3>
1073
1122
< p class ="tip "> < strong > Tip:</ strong > Don't do it. If you absolutely must, first read and understand
1074
1123
1075
1124
1076
- < a href ="http://books.google.com/books?isbn=8131726592 "> < em > Effective Java</ em > Item 7, </ a >
1125
+ < a href ="http://books.google.com/books?isbn=0134686047 "> < em > Effective Java</ em > Item 8 </ a > ,
1077
1126
1078
- "Avoid Finalizers, " very carefully, and < em > then</ em > don't do it.</ p >
1127
+ "Avoid finalizers and cleaners " very carefully, and < em > then</ em > don't do it.</ p >
1079
1128
1080
1129
1081
1130
< a name ="javadoc "> </ a >
@@ -1107,14 +1156,15 @@ <h4 id="s7.1.1-javadoc-multi-line">7.1.1 General form</h4>
1107
1156
1108
1157
</ p > < h4 id ="s7.1.2-javadoc-paragraphs "> 7.1.2 Paragraphs</ h4 >
1109
1158
1110
- < p > One blank line—that is, a line containing only the aligned leading asterisk
1111
- (< code > *</ code > )—appears between paragraphs, and before the group of block tags if
1112
- present. Each paragraph but the first has < code > <p></ code > immediately before the first word,
1113
- with no space after.</ p >
1159
+ < p > One blank line—that is, a line containing only the aligned leading asterisk
1160
+ (< code > *</ code > )—appears between paragraphs, and before the group of block tags if present.
1161
+ Each paragraph except the first has < code > <p></ code > immediately before the first word, with
1162
+ no space after it. HTML tags for other block-level elements, such as < code > <ul></ code > or
1163
+ < code > <table></ code > , are < em > not</ em > preceded with < code > <p></ code > .</ p >
1114
1164
1115
1165
< a name ="s7.1.3-javadoc-at-clauses "> </ a >
1116
1166
1117
- < h4 id ="s7.1.3-javadoc-block-tags "> 7.1.3 Block tags</ h4 >
1167
+ </ p > < h4 id ="s7.1.3-javadoc-block-tags "> 7.1.3 Block tags</ h4 >
1118
1168
1119
1169
< p > Any of the standard "block tags" that are used appear in the order < code > @param</ code > ,
1120
1170
< code > @return</ code > , < code > @throws</ code > , < code > @deprecated</ code > , and these four types never
@@ -1135,8 +1185,9 @@ <h3 id="s7.2-summary-fragment">7.2 The summary fragment</h3>
1135
1185
punctuated as if it were a complete sentence.</ p >
1136
1186
1137
1187
< p class ="tip "> < strong > Tip:</ strong > A common mistake is to write simple Javadoc in the form
1138
- < code class ="badcode "> /** @return the customer ID */</ code > . This is incorrect, and should be
1139
- changed to < code class ="prettyprint lang-java "> /** Returns the customer ID. */</ code > .</ p >
1188
+ < code class ="badcode prettyprint lang-java "> /** @return the customer ID */</ code > . This is
1189
+ incorrect, and should be changed to
1190
+ < code class ="prettyprint lang-java "> /** Returns the customer ID. */</ code > .</ p >
1140
1191
1141
1192
< a name ="s7.3.3-javadoc-optional "> </ a >
1142
1193
< h3 id ="s7.3-javadoc-where-required "> 7.3 Where Javadoc is used</ h3 >
@@ -1150,11 +1201,11 @@ <h3 id="s7.3-javadoc-where-required">7.3 Where Javadoc is used</h3>
1150
1201
< p > Additional Javadoc content may also be present, as explained in Section 7.3.4,
1151
1202
< a href ="#s7.3.4-javadoc-non-required "> Non-required Javadoc</ a > .</ p >
1152
1203
1153
- < h4 id ="s7.3.1-javadoc-exception-self-explanatory "> 7.3.1 Exception: self-explanatory methods </ h4 >
1204
+ < h4 id ="s7.3.1-javadoc-exception-self-explanatory "> 7.3.1 Exception: self-explanatory members </ h4 >
1154
1205
1155
- < p > Javadoc is optional for "simple, obvious" methods like
1156
- < code class ="prettyprint lang-java "> getFoo</ code > , in cases where there < em > really and truly</ em > is
1157
- nothing else worthwhile to say but "Returns the foo".</ p >
1206
+ < p > Javadoc is optional for "simple, obvious" members like
1207
+ < code class ="prettyprint lang-java "> getFoo() </ code > , in cases where there < em > really and truly</ em >
1208
+ is nothing else worthwhile to say but "Returns the foo".</ p >
1158
1209
1159
1210
< p class ="note "> < strong > Important:</ strong > it is not appropriate to cite this exception to justify
1160
1211
omitting relevant information that a typical reader might need to know. For example, for a method
@@ -1179,7 +1230,7 @@ <h4 id="s7.3.4-javadoc-non-required">7.3.4 Non-required Javadoc</h4>
1179
1230
class or member, that comment is written as Javadoc instead (using < code > /**</ code > ).</ p >
1180
1231
1181
1232
< p > Non-required Javadoc is not strictly required to follow the formatting rules of Sections
1182
- 7.1.2, 7.1.3, and 7.2, though it is of course recommended.</ p >
1233
+ 7.1.1, 7.1. 2, 7.1.3, and 7.2, though it is of course recommended.</ p >
1183
1234
1184
1235
1185
1236
0 commit comments