They are so similar that for basic programming they could be considered equivalent. The most obvious thing you might notice is when you add another constructor to PersonDirectory
, like so:
public class PersonDirectory {
private ArrayList<Person> persons;
private DirectoryAdmin admin;
public PersonDirectory() {
persons = new ArrayList<Person>();
}
public PersonDirectory(DirectoryAdmin initialAdmin) {
admin = initialAdmin;
}
}
If you utilize this second constructor, you would find that persons
is null after constructing PersonDirectory. This is because Java does not run other constructors for you automatically. You could fix the problem by adding an explicit call to this()
, which runs also the constructor that matches the signature of the this
call.
public class PersonDirectory {
private ArrayList<Person> persons;
private DirectoryAdmin admin;
public PersonDirectory() {
persons = new ArrayList<Person>();
}
public PersonDirectory(DirectoryAdmin initialAdmin) {
this();
admin = initialAdmin;
}
}
But often times programmers forget to add the call to this(); and may find out too late that persons has been left null because one of their constructors was written carelessly.
If you instead write the initialization inline with the declaration, the initialization is run regardless of which PersonDirectory constructor you call, and so could be considered slightly less error-prone.
public class PersonDirectory {
private ArrayList<Person> persons = new ArrayList<Person>();
private DirectoryAdmin admin;
public PersonDirectory() {
}
public PersonDirectory(DirectoryAdmin initialAdmin) {
// don't have to worry about forgetting to call this();
admin = initialAdmin;
}
}
There are, however, reasons to sometimes prefer initialization in the constructor. For example, it can give more control to subclasses and their constructors.
It is a good practice to declare your member variables final
whenever possible. This way the compiler can remind you if you've written a constructor that leaves some fields uninitialized.
Inline initialization statements always run before the class's constructor(s).