Effective JAVA notes

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:

  • They have names, whereas constructors do not have a separate name.
  • It does not create an object each time we invoke them.
  • It can return an object of any subtype.
  • They reduce the verbosity of creating parameterized type instance.
  • 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.

  • Do not attempt to enforce non-instantiability by making a class abstract as it can be extended.
  • Utility classes where instantiation is not required can be made so by making private constructor.
  • Item 5: Avoid creating unnecessary objects

    Ans.

    Here are some ways to avoid creating unnecessary objects.

  • We can avoid using static factory methods.
  • Unnecessary allocation of expensive objects like Calendar, Time Zone or Date methods. We can put these expensive objects in a static method instead.
  • Prefer primitives to boxed primitives and make sure no unintentional autoboxing happens.
  • Item 6: Eliminate obsolete references

    Ans.

  • When a class manages its own memory, we should be alert. Whenever an element is free to object reference, it should be nulled out.
  • Caches are a common source of memory leak, so whenever using cache use weak hashmap or similar data-structure. Also, a background thread can be scheduled to remove data from a cache.
  • Listeners and other callbacks are also a source of memory leaks. In these cases, we can store keys in weakhashmap.
  • 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.

    Item 25: Obey general contract when overriding equals

    Previous
    Next Post »

    Pages