More on this book
Community
Kindle Notes & Highlights
One advantage of static factory methods is that, unlike constructors, they have names.
A class can have only a single constructor with a given signature. Programmers have been known to get around this restriction by providing two constructors whose parameter lists differ only in the order of their parameter types. This is a really bad idea.
In cases where a class seems to require multiple constructors with the same signature, replace the constructors with static factory methods and carefully chosen names to highlight their differences.
A second advantage of static factory methods is that, unlike constructors, they are not required to create a new object each time they're invoked. This allows immutable classes (Item 15) to use preconstructed instances, or to cache instances as they're constructed, and dispense them repeatedly to avoid creating unnecessary duplicate objects.
A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type.
Interfaces can't have static methods, so by convention, static factory methods for an interface named Type are put in a noninstantiable class (Item 4) named Types.
A service provider framework is a system in which multiple service providers implement a service, and the system makes the implementations available to its clients, decoupling them from the implementations.
There are three essential components of a service provider framework: a service interface, which providers implement; a provider registration API, which the system uses to register implementations, giving clients access to them; and a service access API, which clients use to obtain an instance of the service.
The main disadvantage of providing only static factory methods is that classes without public or protected constructors cannot be subclassed.
A second disadvantage of static factory methods is that they are not readily distinguishable from other static methods.
the telescoping constructor pattern works, but it is hard to write client code when there are many parameters, and harder still to read it.
// Enum singleton - the preferred approach public enum Elvis { INSTANCE;
it is more concise, provides the serialization machinery for free, and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks.
Occasionally you'll want to write a class that is just a grouping of static methods and static fields.
They can be used to group related methods on primitive values or arrays, in the manner of java.lang.Math or java.util.Arrays. They can also be used to group static methods, including factory methods (Item 1), for objects that implement a particular interface, in the manner of java.util.Collections. Lastly, they can be used to group methods on a final class, instead of extending the class.
if a class is accessible outside its package, provide accessor methods, to preserve the flexibility to change the class's internal representation.
if a class is package-private or is a private nested class, there is nothing inherently wrong with exposing its data fields—assuming they do an adequate job of describing the abstraction provided by the class.
All of the information contained in each instance is provided when it is created and is fixed for the lifetime of the object.
Don't provide any methods that modify the object's state (known as mutators).
Ensure that the class can't be extended.
Make all fields final.
Make all fields private.
Ensure exclusive access to any mutable components.
It is known as the functional approach because methods return the result of applying a function to their operand without modifying it. Contrast this to the more common procedural or imperative approach in which methods apply a procedure to their operand, causing its state to change.
Immutable objects are inherently thread-safe; they require no synchronization.
An immutable class can provide static factories (Item 1) that cache frequently requested instances to avoid creating new instances when existing ones would do.
Opting for static factories in place of public constructors when designing a new class gives you the flexibility to add caching later, without modifying clients.
The only real disadvantage of immutable classes is that they require a separate object for each distinct value.
The alternative to making an immutable class final is to make all of its constructors private or package-private, and to add public static factories in place of the public constructors
Classes should be immutable unless there's a very good reason to make them mutable.
make every field final unless there is a compelling reason to make it nonfinal.
It is safe to use inheritance within a package, where the subclass and the superclass implementations are under the control of the same programmers.
Unlike method invocation, inheritance violates encapsulation
Instead of extending an existing class, give your new class a private field that references an instance of the existing class.
The only way to test a class designed for inheritance is to write subclasses.
You can combine the virtues of interfaces and abstract classes by providing an abstract skeletal implementation class to go with each nontrivial interface that you export. The interface still defines the type, but the skeletal implementation takes all of the work out of implementing it.
static member classes, nonstatic member classes, anonymous classes, and local classes.
One common use of a static member class is as a public helper class, useful only in conjunction with its outer class. For example, consider an enum describing the operations supported by a calculator (Item 30). The Operation enum should be a public static member class of the Calculator class. Clients of Calculator could then refer to operations using names like Calculator.Operation.PLUS and Calculator.Operation.MINUS.

