Prefer Lists to Arrays
Lists are invariant whereas arrays are covariant; prefer lists to arrays. (You can’t use generics with arrays, by the way.)
Reading notes of Effective Java, 2nd ed.
Lists are invariant whereas arrays are covariant; prefer lists to arrays. (You can’t use generics with arrays, by the way.)
The lambda expression introduced in Java 8 makes writing the strategy pattern easier.
What’s worse than a class hierarchy? Tagged class.
Don’t do this and don’t implement it for your class.
Most likely, static member class or lambda expression is sufficient.
Inheritance is safe only within a package or when a class is explicitly designed for extension.
Java 8 introduces interface default method, making the most common use of abstract classes, skeletal implementation, less needed.
POD (plain-old data) classes should never be exported (public or protected); use getter/setter instead.
Make a class immutable when you can (and as immutable as possible when you can’t).
Make each class or member as inaccessible as possible.
Comparable has a contract similar to equals’s.
If you need to copy an object, add your own copy constructor or factory method; don’t use clone.
TL;DR Always override toString.
You must override hashCode in every class that overrides equals.
If you need logical equality (instead of object identity), override equals(), and obey the contract when overriding it (think of value classes).
It is almost certainly wrong to use finalizers.
Reuse is the key to avoid creating unnecessary objects.
Unintentional object retention (or informally called memory leaking) can be fixed by nulling out references once they become obsolete.
To prevent someone instantiating an utility class, use private constructor.
The most common way is using private constructor with static final member or factory method.
Static factory methods let you implement object pool or return sub-types, which is useful in a service-provider scenario. However, be careful about the open-and-init anti-pattern.
Use a builder for breaking down constructor parameters, or as a closure of a constructor.
Don’t do it. It is a bad idea.