Friday 8 July 2022

Advantage of Singleton Pattern over Static Class in Java

 

Main advantage of Singleton over static is that former is more object oriented than later. With Singleton, you can use Inheritanceand Polymorphism to extend a base class, implement an interface and capable of providing different implementations. If we talk about java.lang.Runtime, which is a Singleton in Java, call to getRuntime() method return different implementations based on different JVM, but guarantees only one instance per JVM, had java.lang.Runtime an static class, it’s not possible to return different implementation for different JVM.


That’s all on difference between Singleton and static class in Java. When you need a class with full OO capability , chose Singleton, while if you just need to store bunch of static methods together, than use static class.

No comments:

Post a Comment