Errata

Java in a Nutshell

Errata for Java in a Nutshell, Eighth Edition

Submit your own errata for this product.

The errata list is a list of errors and their corrections that were found after the product was released. If the error was corrected in a later version or reprint the date of the correction will be displayed in the column titled "Date Corrected".

The following errata were submitted by our customers and approved as valid errors by the author or editor.

Color key: Serious technical mistake Minor technical mistake Language or formatting error Typo Question Note Update

Version Location Description Submitted By Date submitted Date corrected
Page Table3.2
final in Modifier column Field in the Used on column

The description for Field reads as follows:
Field The field cannot have its value changed. static final fields are compile-time constants.
The sentence "static final fields are compile-time constants" is not completely correct.
A static final field may have been initialized in the class initializer block(static block) in which
case it is a runtime constant. only if the static final field is initialized at declaration then it
is a compile-time constant
e.g.
public class SomeClass {
public static final int x;
public static final int y = 7;
static {
x = 5; // in this case x is not a compile-time constant
}
public void someMethod() {
int z = 5;
switch (z) {
case x: // would fail to compile, since x is not a compile-time constant

case y: // would compile as y is a compile-time constant
}
}
}

Note from the Author or Editor:
Good catch. This has been corrected in the upcoming edition.

Pravin Jain  Jul 05, 2023 
Page The java type system -> Nested types -> Static member types
The last item of the bullet list of the first paragraph

"A static member type may not be defined within any other kind of nested type."

This seems to be wrong. The following example compiles fine in java 17 for me:

class A {
class B {
static class C {
}
}
}

Note from the Author or Editor:
Good catch. This restriction was relaxed with the introduction of Records in JDK 16 it looks like. Updating the text to reflect this for the coming edition. Thank you!

Anders Granlund  Feb 21, 2026 
Page Object-Oriented Programming In Java -> Data Hiding and Encapsulation -> Access Control -> Access to
One item in the bullet list of the 3rd paragraph

"If a member of a class is declared private, the member is never accessible, except within the class itself. This is the most restrictive type of access control."

This does not seem to be true. This compiles fine for me in java 17 for example:

class A {
static class B {
private static int x = 1;
}

static class C {
void f() { B.x = 2; }
}
}

The general rule seems to be that private members of one type is accessable from another type iff both types are nested arbitarily deep inside the same top-level type.

Note from the Author or Editor:
You're correct, visibility is also extended to nested types within the class. This will be corrected in the upcoming edition.

Anders Granlund  Feb 21, 2026 
Page The Java Type System -> Nested Types -> Local Classes -> Features of local classes
Items of the bullet list of the 2nd paragraph

"Like member classes, and for the same reasons, local classes cannot contain static fields, methods, or classes. The only exception is for constants that are declared both static and final."

This does not seem to be true.

"Interfaces, enumerated types, and annotation types cannot be defined locally."

This does not seem to be true. Interfaces and enumerated types seems to be possible to define locally.

Note from the Author or Editor:
This is correct and will be fixed in the upcoming edition. The rules for where those types are allowed have relaxed in recent JDK releases.

Anders Granlund  Feb 21, 2026 
Page 105
last paragraph before summary

Currently the second sentence of the paragraph reads as follows:
You still must define a class matching the name of the file and a main() method, but then you can execute the program with:
java MyClass.java

I have tested on Linux Jdk version 17, and found that name of the class may not match the file name still it works. It expects the first type definition(class, interface, enum or record) to contain the main method, and executes that main. e.g.
In a file named Hello.java, if I define class as follows:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
and then give the command
java Hello.java
it still executes the main found in HelloWorld class.

Note from the Author or Editor:
Thanks for the catch. This text has been significantly updated in the upcoming 9th edition and corrects this issue.

Pravin Jain  Jul 03, 2023 
Page 337
Class 'BirthdayDiary' / method 'getDaysUntilBirthday'

I don't think that method does what it's supposed to:
* We are interested in the next birthday, not the original one.
* The value may be negative if this year's birthday has already passed.
* Period's 'getDays' only returns the days-portion (as opposed to months / years), NOT the entire period in days.

Maybe something like this would suffice?
[code]
public long getDaysUntilBirthday(String name) {
LocalDate today = LocalDate.now();
LocalDate bd = birthdays.get(name);
LocalDate bdThisYear = bd.withYear(today.getYear());
return ChronoUnit.DAYS.between(
today,
today.isBefore(bdThisYear) ? bdThisYear : bdThisYear.plusYears(1)
);
}
[/code]

Andreas Beer  Apr 14, 2026 
Page 338
4th line from bottom

The third quarter goes to (excluding) October, not November.

Note from the Author or Editor:
Good find. Fixed in upcoming edition.

Andreas Beer  Apr 15, 2026