Most of the time, when I'm writing Java code, I setup the dependency externally and don't give classes a default if it is null. Instead, if it's null, I throw an IllegalArgumentException to force the value to be set. Generally, I do something like the following:
public class Foo {
private final Dependency dependency;
public Foo(Dependency dependency) {
if(dependency == null) {
throw new IllegalArgumentException("dependency must be set message.");
}
this.dependency = dependency;
}
// the rest of the code here.
}
With the variable set to final, it will force the value to be set in the constructor, and by throwing an exception, if the developer attempts to pass null, you will help the programmer help to not miss this requirement quickly, rather than a null pointer exception later down the road. If you are using something like spring with an application context XML file to do dependency injection, then this will also help you to track down misspellings or other problems in the xml file, since it will immediately fail, if you fail to provide the parameter.