Effective Java

While browsing around the net, I came across this free Java book. It was a PDF whose chapters each contain samples from other books. One such sample chapter was from Effective Java by Joshua Bloch. The second edition of this book came out last year. The first edition was from back in 2001.

Although Bloch now works for Google, he came from Sun Microsystems. This is why he is intimately familiar with Java and the Java API. The book has a list price of $49.99 like the first edition. How you can find discounts at many places like Amazon dot com.

Here are some interesting ideas I got from Chapter 2 of the book. The first is that of a static factory method. This is a method which is static. It returns an instance of the class. This technique has a number of advantages over a normal constructor. This is not to be confused with the Factory Method design pattern.

One thing I found surprising was talk about the protected access modifier. When I learned Java, I was only taught about public and private. However now that I think back upon it, someone in my class who already knew some Java asked whether "protected" had been removed from Java. Unfortunately the instructor did not know the answer.

Another pattern described is the Builder pattern. This is a three part process to initialize objects with a subset of all possible parameters. First you get a builder object. You then call setters on this object for the optional parameter you wish to set. Then you call the build object which uses the properties you set, and generates an object for you.

Finally there is a call for developers to remember to null out references to objects that are obsolete. This is essential to prevent memory leaks. Yes there is potential for memory leaks in Java, especially when you are managing your own objects as in a stack.