Effective Java – Part 1

This is the first part of the book summary based on Effective Java book by Joshua Bloch.

Chapter 1. Introduction

  • To use a language effectively, you need to master its grammar, vocabulary and usage.
  • How to make effective use of java and some of its libraries.
  • This book is not for beginners.
  • Fundamental Principles – Clarity, Simplicity, No Surprises, Reuse, Fail Fast.
  • Write programs that are clear, correct, usable, robust, flexible, and maintainable

Chapter 2. Creating and Destroying Objects

Item 1: Consider providing static factory methods instead of constructors
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. Static factory methods do not have this limitation. Providing two constructors whose parameter lists differ only in the order of their parameter types is a bad idea. 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. A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type.

The main disadvantage of 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.

valueOf returns an instance that has, loosely speaking, the same value as its parameters.
getInstances returns an instance that is described by its parameters but cannot be said to have the same value.

Item 2: Enforce the singleton property with a private constructor
Two approaches to implementing singletons are usinga a public static final field and using a static factory method.
To make a Singleton serializable, provide a readResolve method.

Item 3: Enforce noninstantiability with a private constructor
In the absence of explicit constructors in a class,the compiler provides a public, parameterless default constructor. Attempting to enforce noninstantiability by making a class abstract does not work. A class can be made noninstantiable by including a single explicit private constructor. This also prevents the class from being subclassed

Item 4: Avoid creating duplicate objects
An object can always be reused if it is immutable (Item 13). You can often avoid creating duplicate objects by using static factory methods (Item 1).
You can reuse mutable objects that you know will not be modified. An adapter is one object that delegates to a backing object, providing an alternative interface to the backing object.Because an adapter has no state beyond that of its backing object, there’s no need to create more than one instance of a given adapter to a given object.

Avoiding object creation by maintaining your own object pool is a bad idea unless the objects in the pool are extremely heavyweight.

Item 5: Eliminate obsolete object references
An obsolete reference is simply a reference that will never be dereferenced again. Null out references once they become obsolete or reuse the variable in which it was contained or to let it fall out of scope.
Whenever a class manages its own memory, the programmer should be alert for memory leaks.

Item 6: Avoid finalizers
Don’t use finalizers except as a safety net or to terminate noncritical native resources.Provide an explicit termination method instead. Nothing time-critical should ever be done by a finalizer. Never depend on a finalizer to update critical persistent state.

Explicit termination methods are often used in combination with the try-finally construct to ensure prompt termination.

Finalizers are unpredictable, often dangerous, and generally unnecessary. There is no guarantee that finalizers will be executed promptly or executed at all.

If an uncaught exception is thrown during finalization, the exception is ignored,and finalization of that object terminates

If a class (other than Object) has a finalizer and a subclass overrides it, the subclass finalizer must invoke the superclass finalizer manually.
If you need to associate a finalizer with a public, nonfinal class, consider using a finalizer guardian to ensure that the finalizer is executed, even if a subclass finalizer fails to invoke super.finalize

Leave a Comment