The code you have is nicely formatted, and well documented, etc. ... but, as a singleton, it has a number of problems....
The two most glaring are:
Earth real = Earth.getInstance();
Earth.setEarth(null);
Earth alternate = Earth.getInstance();
if (real != alternate) {
System.out.println("Oops...");
}
The second most glaring issue is the synchronization.
You suggest that the getInstance() needs to be synchronized to avoid thread problems... but your other setter/getter methods are not synchronized.... as a result, threads all over the place can be getting stale, wrong, and otherwise incomplete populations, ages, etc.
It is not a singleton
Earth real = Earth.getInstance(); Earth.setEarth(null); Earth alternate = Earth.getInstance(); if (real != alternate) { System.out.println("Oops..."); }The synchronization.
You suggest in your comments that the
getInstance()needs to be synchronized to avoid thread problems... but your other setter/getter methods are not synchronized.... as a result, threads all over the place can be getting stale, wrong, and otherwise incomplete populations, ages, etc.
As an example of a singleton 'best use case', this one has some problems... ;-)
But, that's sort of OK, since Earth has problems anyway!