Effective Java
Rate it:
Read between July 9, 2013 - January 12, 2014
5%
Flag icon
One advantage of static factory methods is that, unlike constructors, they have names.
5%
Flag icon
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.
5%
Flag icon
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.
5%
Flag icon
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.
5%
Flag icon
A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type.
5%
Flag icon
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.
6%
Flag icon
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.
6%
Flag icon
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.
6%
Flag icon
The main disadvantage of providing only static factory methods is that classes without public or protected constructors cannot be subclassed.
6%
Flag icon
A second disadvantage of static factory methods is that they are not readily distinguishable from other static methods.
7%
Flag icon
the telescoping constructor pattern works, but it is hard to write client code when there are many parameters, and harder still to read it.
8%
Flag icon
// Enum singleton - the preferred approach public enum Elvis {     INSTANCE;
8%
Flag icon
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.
8%
Flag icon
Occasionally you'll want to write a class that is just a grouping of static methods and static fields.
8%
Flag icon
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.
22%
Flag icon
if a class is accessible outside its package, provide accessor methods, to preserve the flexibility to change the class's internal representation.
22%
Flag icon
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.
22%
Flag icon
All of the information contained in each instance is provided when it is created and is fixed for the lifetime of the object.
22%
Flag icon
Don't provide any methods that modify the object's state (known as mutators).
22%
Flag icon
Ensure that the class can't be extended.
23%
Flag icon
Make all fields final.
23%
Flag icon
Make all fields private.
23%
Flag icon
Ensure exclusive access to any mutable components.
23%
Flag icon
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.
23%
Flag icon
Immutable objects are inherently thread-safe; they require no synchronization.
23%
Flag icon
An immutable class can provide static factories (Item 1) that cache frequently requested instances to avoid creating new instances when existing ones would do.
23%
Flag icon
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.
23%
Flag icon
The only real disadvantage of immutable classes is that they require a separate object for each distinct value.
24%
Flag icon
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
24%
Flag icon
Classes should be immutable unless there's a very good reason to make them mutable.
24%
Flag icon
make every field final unless there is a compelling reason to make it nonfinal.
24%
Flag icon
It is safe to use inheritance within a package, where the subclass and the superclass implementations are under the control of the same programmers.
25%
Flag icon
Unlike method invocation, inheritance violates encapsulation
25%
Flag icon
Instead of extending an existing class, give your new class a private field that references an instance of the existing class.
27%
Flag icon
The only way to test a class designed for inheritance is to write subclasses.
28%
Flag icon
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.
31%
Flag icon
static member classes, nonstatic member classes, anonymous classes, and local classes.
31%
Flag icon
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.