Skip to content

Commit 6d8f83c

Browse files
committed
1 parent 7f7439e commit 6d8f83c

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

src/effectivejava/chapter11/item83/Initialization.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
// Initialization styles - Pages 333-
44
public class Initialization {
55

6-
// Normal initialization of an instance field - Page 282
6+
// Normal initialization of an instance field4 - Page 282
77
private final FieldType field1 = computeFieldValue();
88

9-
// Lazy initialization of instance field - synchronized accessor - Page 333
9+
// Lazy initialization of instance field4 - synchronized accessor - Page 333
1010
private FieldType field2;
1111
private synchronized FieldType getField2() {
1212
if (field2 == null)
@@ -27,15 +27,17 @@ private static class FieldHolder {
2727

2828
private FieldType getField4() {
2929
FieldType result = field4;
30-
if (result == null) { // First check (no locking)
31-
synchronized(this) {
32-
if (field4 == null) // Second check (with locking)
33-
field4 = result = computeFieldValue();
34-
}
30+
if (result != null) // First check (no locking)
31+
return result;
32+
33+
synchronized(this) {
34+
if (field4 == null) // Second check (with locking)
35+
field4 = computeFieldValue();
36+
return field4;
3537
}
36-
return result;
3738
}
3839

40+
3941
// Single-check idiom - can cause repeated initialization! - Page 334
4042
private volatile FieldType field5;
4143

src/effectivejava/chapter6/item39/regularannotation/ExceptionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
@Retention(RetentionPolicy.RUNTIME)
1818
@Target(ElementType.METHOD)
1919
public @interface ExceptionTest {
20-
Class<? extends Exception>[] value();
20+
Class<? extends Throwable>[] value();
2121
}

src/effectivejava/chapter6/item39/regularannotation/RunTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public static void main(String[] args) throws Exception {
2929
} catch (Throwable wrappedExc) {
3030
Throwable exc = wrappedExc.getCause();
3131
int oldPassed = passed;
32-
Class<? extends Exception>[] excTypes =
32+
Class<? extends Throwable>[] excTypes =
3333
m.getAnnotation(ExceptionTest.class).value();
34-
for (Class<? extends Exception> excType : excTypes) {
34+
for (Class<? extends Throwable> excType : excTypes) {
3535
if (excType.isInstance(exc)) {
3636
passed++;
3737
break;

src/effectivejava/chapter6/item39/repeatableannotation/ExceptionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
@Target(ElementType.METHOD)
1313
@Repeatable(ExceptionTestContainer.class)
1414
public @interface ExceptionTest {
15-
Class<? extends Exception> value();
15+
Class<? extends Throwable> value();
1616
}

0 commit comments

Comments
 (0)