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.");
    }
}
1

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

2 Comments

without the synchronized getInstance method couldn't a multi-threaded application potentially run into issues during instance creation and create multiple instances?
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.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.