0

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.");
    }
}
c12
  • 9,557
  • 48
  • 157
  • 253
  • Does not answer your question, but check out this answer too: http://stackoverflow.com/questions/70689/efficient-way-to-implement-singleton-pattern-in-java – SirPentor Jun 08 '12 at 18:27

4 Answers4

2

No, not really. As the constructor is private it cannot be subclassed anyway. It just makes the fact more explicit, which is why I'd use final personally.

Matthias Meid
  • 12,455
  • 7
  • 45
  • 79
1

Please see this,

When you do this:

private static Singleton instance = new Singleton();

ie. initializing the object during declaration in your above code.

then, there is no need to use synchronized on the getInstance() method, Dont worry. still singleton is implemented with 100% assurance.

just do this:

 public static Singleton getInstance() {
        return instance;
    }

Final keyword will just make sure that the class can Not be extended..thats it.. It Will Not effect the singleton pattern.

You can use the Double Check Locking also to implement Singleton Pattern, Please refer HEAD FIRST DESIGN PATTERN

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • without the synchronized getInstance method couldn't a multi-threaded application potentially run into issues during instance creation and create multiple instances? – c12 Jun 08 '12 at 18:46
  • 2
    It will never... i can bet my life on it... synchronized keyword will be needed only and only if.. the static object reference variable is NOT initialized during its declaration, like private static Singleton instance; and then you need to have a method like this public static synchronized Singleton getInstance() { if (instance == null){ instance = new Singleton(); } return instance; } . Please refer javaranch.com or head first design pattern, if you have doubt abt it. – Kumar Vivek Mitra Jun 08 '12 at 18:48
1

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.

Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
0

No. Class is already singleton pattern. Final modifier makes class non-derivable but it is already non-derivable as it has private constructor.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103