Home » Java » Effective Java reading notes (a): class, interface...

Effective Java reading notes (a): class, interface, object


"someone might think I don't need any books on Java, but I need this one" - James Gosling


Java field is regarded as some of the books of the Bible, which (I think) two of the most famous is the and . if Java programming Java taught us the language, so Effective Java teaches us how to write better Java language program.

this book for API programmers to develop more counterparts, but for ordinary API users also has a very large value. The basic reading notes will extract the original books, according to their own in order to organize the entries in the book, and some rare items will ignore.

Class and interface
1. minimize the accessibility of classes and members

as much as possible so that the members of each class or by external access, consistent with the use of the corresponding function you are writing software, the minimum possible level of access to the private. Do not default, the default public class can not, can not open the internal
Public class (public) should never be exposed to variable domain, because if you do not change the API cannot change its data representation. If you need to access the domain by providing access methods, namely getter/setter. with one exception, namely public static final (public static final) domain, which is a constant the morphology of.

2. levels of choice

composite (composition) prior to the succession, only when the class is really a parent class sub types, namely the son with the father as "is-a", you should use inheritance, otherwise normally, "a subclass instance" contains "superclass", "superclass" forwarding message. "Br>
Interfaces take precedence over abstract classes, and interfaces are usually the best choice for defining multiple implementations, and abstract classes should be used only when evolution is more important than flexibility and functionality.

Class hierarchies take precedence over tag classes, and label classes can be interpreted as verbose switch-case/if-else statements, and in general, they can be reconstructed using the polymorphic nature of Java, that is, the policy schema.

3. internal classes
There are four classes of

internal classes: static member classes, non static member classes, anonymous classes, local classes,.

  • static member class can be considered a generic class, and the common usage is as a helper class, which is meaningful only when used with an external class.

  • Each instance of
  • non static members of the class are implicitly associated with a peripheral instance of the enclosing class, and this reference may lead to the enclosing class in accordance with GC conditions by inner classes and cannot be GC. if you do not need external examples, give priority to the use of static members of the class.

  • The common use of
  • anonymous classes is to dynamically create function objects and implement policy.

  • The
  • local class is similar to the local variable, and the scope rules are the same, and are rarely used in general... Anonymous classes and local classes defined in the non static environment is peripheral examples, and cannot contain static members of.

Constructors and objects
Use the Builder schema when more than 1. constructor parameters are used

Android developers used the Square open source project, the Builder model must not strange, by chaining finally build out of a target object. When most of the parameters are optional when using the Builder model is more elegant way, if you can use the same Builder model parameter is necessary, but it is necessary to do the work of.

2. enumeration to implement the Singleton property

Singleton represents the only class object. The spread of the Internet has a variety of single case writing, there is a famous example of the "single in 7 ways, the book is written, recommend using static members, public and private can be, you need to provide a private access method.

public, class, Singleton {
public/private, static, final, Singleton, INSTANCE = new, Singleton ();
private (Singleton) {}

however, when it comes to serialization, the above method requires additional action. The better practice is to use enumeration.

public, enum, Singleton {
private int mFile;
public, void, method ();

3. consider using static factories instead of constructors

has several advantages over doing this:

  • static factory has a name;

  • can do so without having to call each time they create a new object, such as the singleton model,

  • can return a subclass object;

  • creates an instance of a generic class that makes the code simple,.

    public, static, , HashMap, newInstance () {
    return, new, HashMap ();

4. avoid creating unnecessary objects

  • can instantiate an object only once and try not to instantiate many times, especially in the loop body;

  • pays attention to automatic boxing, so that the basic types are boxed into objects, especially in the loop body,.

  • The most classic use of
  • pool technology is String, String, STR = foo, and String, STR = new, String (` foo '), , the former uses the constant pool, and the latter instantiated an object.

5. eliminate expired object references

is closely related to this topic and memory leaks, and memory leaks is another big topic, so here not to discuss the memory leak. In short, if an object is useless, it should be clear to the referenced object reference set to null. when the programmer was the first time such problems, often caution: for each object reference, once when they need not set to null, but the empty object references should be an exception rather than the norm, eliminate outdated reference the best way is to let the reference variable contains the end of their life cycle.

6. avoid terminating methods

need to use try-finally structure to clean up resources, cleaning method was invoked in finally, such as input and output flow close method. The end method is unpredictable, it is very dangerous, should avoid the use of end method.

7. cover equals and hashCode methods

Object class of these two methods the coverage is very particular, but IDE has helped us to solve. We want to know is: when the concept of class is equal to the logic, which is more content not refer to the same object, you need to override the equals method; the general need to override the hashCode method in each covering the equals method of the class, otherwise the hash of the set of possible error. Based on these two methods can see more discussion before a blog Java:equals and hashCode