4

Can somebody, please explain me clearly on java singleton design pattern class vs java final class.

Can we use a final class instead of using a singleton class?

For a final class we can create the instance of the class and use the methods in that, but not overriding properties, if the methods are static in the final class, then we can just call the methods like ClassName.getMethod, why should we go for singleton instead of final class?

Attila
  • 3,206
  • 2
  • 31
  • 44
Raju Sidda
  • 118
  • 1
  • 7
  • 1
    You can't extend a final class but you can extend the singleton class.. – Shailendra Madda Nov 28 '16 at 18:35
  • 1
    The two concepts are completely orthogonal. – yshavit Nov 28 '16 at 18:37
  • since static variables cannot be overwritten even by subclasses the question basically boils down to: why use a singleton instead of static variables of a class!? – luk2302 Nov 28 '16 at 18:41
  • @luk2302 Using static variables doesn't mean ensuring that only one instance of a class is created. Btw you need a static variable to to create a singleton so I don't think your comment is very accurate..or I didn't understand it properly :) – C.Champagne Nov 28 '16 at 18:49
  • @C.Champagne *I* know exactly what the difference is :P I was trying to get OP (and the other answers) into the right direction. Imho mentioning final only introduces an unnecessary keyword in this case because OP basically asks: why use a singleton instead of a statics in a (final) class. – luk2302 Nov 28 '16 at 18:51

5 Answers5

10

A final class is one which cannot be extended. You can have any number of instances in a final class. The best example of a final class is String. Singleton is a single instance of a class. So both are not exactly same, however you can make your singleton class final to restrict someone from extending it.

Raghav
  • 4,590
  • 1
  • 22
  • 32
  • 1
    Singleton classes, if properly implemented, do not need to be declared final in order to not be extended. Since all the constructors of the singleton class are private, you can't extend that class. In fact, singleton classes are effectively final. – Enrico Giurin Jan 26 '17 at 04:19
3

You can't extend a final class and there is only single instance for a singleton class. So inheritance concept is not applicable for the final class. But you can create any number of instances in final class but you can't in Singleton.

i.e, final class is immutable class, can create more than one instance but where as Singleton class has only single instance.

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
1

A singleton is a class with a single instance for all the life of your program. But a final class, you can have multiple instances of it. The thing is that you cannot inherit from a final class, but nothing stops you to have a lot of instances of it.

JFPicard
  • 5,029
  • 3
  • 19
  • 43
0

A singleton class is usually tied to the ClassLoader. Basically, you should have one per JVM. A final class is something you can't inherit from, but just because it's final it doesn't mean you can't have multiple instances of it (per JVM).

Notice that, when I say per JVM I'm talking about, for instance, a .class file in a .war file deployed once; if the same Web application is deployed multiple times, each one gets its own singleton.

x80486
  • 6,627
  • 5
  • 52
  • 111
-1

I think your question is more about static versus singleton. Being a final class is not relevant in this context since you cannot override a static method (you can of course define the same signature static method in the extension but this would be irrelevant because the biding will still be "static" in the sense that calling SuperClass.getMethod will always call the definition from SuperClass). This is a classic question and I am sure you can find it answered elsewhere. However, just to give you a short answer, I would say that using a singleton allows you to implement interfaces and pass around your singleton object like any other object.

Maybe I can explain, to whomever gave me -1, what I meant (I thought it was clear enough). The original question is about singleton vs final. But when he explains, he states clearly that he is going to use static methods. He doesn’t realize then that what makes the methods unique in the sense that they belong to a single class is the fact that they are static, and so are class methods instead of instance methods. Being final has no effect: the user can instantiate as many as he wants, it only prevents him from extending the class, which did not seem to me to be the true concern of who asked the question. On the other hand, he clearly states that he will use static methods on his final class. This seems then to indicate he wants to make it a singleton by using static methods: this is a classic way of doing it, and the question of using static methods versus singleton has been asked before. See for instance Why use a singleton instead of static methods? or Difference between static class and singleton pattern? When I then write about overriding a static method, I try to explain that static method are not really overridden in the sense that they are not involved in dynamic binding. To give an example, if you have class A with static method foo() and then class B extends class A and redefines static foo(), that’s OK. But calling foo() on an object obj will call the static method belonging to the type of obj, even if you assign it a different object at runtime (this is what I call static binding). In other words, if you declare “A obj = new B()” and call obj.foo() it will still call A.foo() I don’t know if that explanation helps, I wish whoever gave me -1 explained why.

Community
  • 1
  • 1
Ekeko
  • 1,879
  • 14
  • 15