Immutable Classes in Java: 5 Rules, the Real Cost, and Why Records Leak | Effective Java L18
The Modern SDE
0:00 / 0:00
Immutable Classes in Java: 5 Rules, the Real Cost, and Why Records Leak | Effective Java L18
3 просмотра · 13 часов назад
The Modern SDE
1,24 тыс. подписчиков
3 просмотра · 13 часов назад
Lecture 18 of the Effective Java Masterclass by Engineer X · Part 3: Classes and Interfaces.
In this lecture
• A record whose list changes after it was built, shown in a memory view
• The five rules of an immutable class, including defensive copies in and out
• The functional approach: methods return new values (plus, not add) and the BigDecimal.add trap
• Final classes vs private constructors with static factories; why BigInteger and BigDecimal aren't final
• Benefits: one state, thread safety without locks, free sharing, no copies, safe map keys, failure atomicity
• The cost of one object per value, multi-step methods, and mutable companions like StringBuilder
• When a class must be mutable: few states, final fields, complete constructors
• Modern Java: records are only shallowly immutable; List.copyOf vs unmodifiableList vs Stream.toList
• Interview: how do you make a class immutable, and is a record immutable?
Chapters
0:00 An order that changed itself
1:02 What immutable means
2:08 An immutable class, written out
3:59 Why immutable wins
4:48 The cost of a new object
6:24 Modern Java: records are shallow
7:48 In the interview
8:28 Recap
8:58 Quiz
Follows the topics of Effective Java, 3rd edition, by Joshua Bloch (Item 17: Minimize mutability). Every explanation, example and line of code in this lecture is original.
Previous lecture: L017 Java Records vs Getters and Setters: What Public Fields Really Cost
Next lecture: L019 Composition over Inheritance in Java: The Bug Java 21 Exposed
#Java #EffectiveJava #JavaInterview #SeniorJavaDeveloper #JavaBestPractices #EngineerX #JavaImmutability #ImmutableClass #JavaRecords