liveBook · Manning

Initializer

As explained in 100 Java Mistakes and How to Avoid Them

A constructor is a special Java member that initializes a newly created object and may throw exceptions or produce side effects.

A Java constructor initializes a newly created object. It can also have side effects or throw exceptions, so a `new` expression may occasionally be used only for registration or validation. That flexibility is risky: creating an object and discarding it usually means a missing assignment, return, or `throw`. section 2.12

Creating an exception is not throwing it

`new IllegalArgumentException("Value is negative");` merely constructs an exception object. Unless it is thrown, it is silently discarded and execution continues. Prefer `throw new IllegalArgumentException(...)` when validation must stop the method. section 2.12

Constructor references can hide which overload receives a functional-interface argument. In `contents.computeIfAbsent(fileName, StringBuilder::new)`, the map key is passed to `StringBuilder(String)`, so each builder begins with its filename. Use `s -> new StringBuilder()` when an empty builder is intended; converting a reference to a lambda makes the passed argument visible. section 2.13

A char selects capacity, not content

`new StringBuilder('[')` invokes `StringBuilder(int)`: the character is widened to its numeric value and becomes initial capacity, rather than initial text. Use `new StringBuilder("[")` or create an empty builder and append the character. Static analysis can reliably flag this pattern. section 9.1

Avoid initializer-related mistakes by following these rules:

  • Keep constructor side effects and complex validation rare, so discarded `new` expressions are suspicious and can be linted. section 2.12
  • Make constructor-reference inputs explicit with a lambda when overload selection or ignored arguments are unclear. section 2.13
  • Prefer string literals over character literals when supplying initial `StringBuilder` content, and test user-visible messages. section 9.1
  • Treat object construction separately from class initialization: field initializers run in declaration order, while circular class initialization can expose null values or deadlock across threads. section 3.9.3
StringBuilder sb = new StringBuilder('[');    #1
sb.append(value);
sb.append(']');

This short call isolates overload selection: the character literal is converted to an integer capacity instead of becoming builder content. section 9.1

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains concrete failure modes and corrections, while deferring general constructor implementation internals.
Examples
Uses a discarded IllegalArgumentException, a computeIfAbsent StringBuilder factory, and a char-based StringBuilder call.
What it assumes
Assumes familiarity with Java object creation; introduces the relevant overload and method-reference pitfalls through focused examples.
What you can do after
Diagnose/debug with it by spotting discarded constructions and unintended constructor-overload selection.
How it connects
Connects constructors with exceptions, method references, functional interfaces, StringBuilder, field initializers, and class initialization.

Also explained in

Compare how 9 books explain initializer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Java Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.