Does making the below class final (adding public final class) have any real impact on this classes intent to implement the singleton pattern?
package org.kodejava.example.pattern.factory;
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton() {
}
public static synchronized Singleton getInstance() {
return instance;
}
public void doSomething() {
for (int i = 0; i < 10; i++) {
System.out.println("i = " + i);
}
}
@Override
protected Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException("Clone is not allowed.");
}
}
When you create a singleton, you try by all means to enforce the pattern. That means, there should be no ways whatsoever to create a second instance of the singleton class.
By declaring a private constructor you ensure no other top level class can extend your Singleton class, but an inner class or static inner class could extend it and break your singleton because an inner class have access to the private members of its enclosing class.
Evidently, this could not happen unless you really wanted it so, but, yet again, if you want to make your singleton bullet proof, if you declare it final, you could be preventing, by all means possible, that the pattern is broken.