We have tried to put all the points from effective java book in shorts. This is one of the best books for java developers by Joshua Bloch. For complete understanding please purchase the book from below link
Item 1: Consider Static Factory methods instead of constructors
Ans.The author is suggesting to have the option of creating a static factory method instead of creating a constructor wherever possible. Like in the Boolean object it has Advantages of Static Factory method are:
Item 2:Consider builder when faced with many constructor parameters
Ans.We should use a builder design pattern, when we have many constructors, as it is difficult to provide so many constructor arguments for client code. This is done by creating a static class and initializing the optional fields with default values. Below is an example of a Builder design Pattern.
Item 3: Enforce singleton property with a private constructor or an enum type
Ans.Item 4: Enforce non-insatiability with a private constructor
Ans.
Item 5: Avoid creating unnecessary objects
Ans.Here are some ways to avoid creating unnecessary objects.
Item 6: Eliminate obsolete references
Ans.
Item 7: Avoid finalizers
Ans.We should avoid finalizers because there is no guarantee that it will be executed.
Also, There can be a performance problem because of finalizers. So always try to close your objects in try-finally block instead of using finalizer.
Item 8: Minimize the accessibility of classes and members
Ans.Item 9: In public classes use accessor methods, not public fields.
Ans.Item 10: Minimize mutability
Ans.Item 11: Favor composition over inheritance.
Ans.Item 12: Design and document for inheritance or else prohibit it.
Ans.Item 13: Prefer interfaces to abstract classes
Ans.Item 14: User interfaces only to define types
Ans.Item 15: Prefer class hierarchies to tagged classes
Ans.Item 16: Use function objects to represent strategies
Ans.Item 17: Favor static member classes over nonstatic
Ans.Item 18: Check parameters for validity
Ans.Item 19: Make defensive copies when needed
Ans.Item 20: Design method signature carefully
Ans.Item 21: Use overloading judiciously
Ans.Item 22: Use varargs judiciously
Ans.Item 23: Return empty arrays or collections not nulls
Ans.The author has tried to tell us here that, returning nulls leads to many errors such as client might forget to handle null, also it complicated the method that returns array or collection.
To do same we can used Collections.toArray(), Collections.emtySet(), Collections.emptyList() or Collections.emptyMap() method can be used.
Item 24: Write doc comments for all exposed API elements.
This itself is self-explanatory, points to be noted here are below.
- The doc comment for a method should describe the contract between method and client.
- The java doc @code is preferable as it eliminates the use of HTML tags.
- No two members or constructors in a class or interface should have the same summary description.
- When documenting a generic make sure to document all type parameters.
- When documenting enum type document constants.
- When documenting annotation document any members as well type itself.
ConversionConversion EmoticonEmoticon