Singleton Pattern vs Static Class (a class, have all Static methods) is another interesting questions, which I missed While blogging is about interview questions on Singleton pattern in Java. Since both Singleton pattern and static class provides good accessibility, and they share some similarities e.g. both can be used without creating object and both provide only one instance, at very high level it looks that they both is Intende D for same task. Because of similarities, interviewer normally ask questions like,
Why do I use Singleton instead of Static Methods, or Can you replace Singleton with static class, And what is differences Between singleton PATTERN&N Bsp;and static in Java. In order to answer these question, it's important to remember fundamental difference between SINGLETON PATTERN&N Bsp;and Static class, former Gives you An object, while later just provide static methods. Since an object was always much more capable than a method, it can guide if you would use Singleton pattern v s static Methods. in This Java article we'll learn, where to use Singleton pattern in Java, and whe n Static class is better alternative. By the To, JDK has examples of both Singleton and Static, and that too very intelligently e.g. java.lang.math i S a final Class with Full of static methods, on the other hand java.lang.runtime is a Singleton class in Java. For those who is not familiar with Singleton design pattern or static Class, static class is A java Class,which only contains static methods, good examples of static Class Is java.lang.math,which contains lots of utility methods for various maths function E.g. sqrt (). While singleton Classes are Those, which have only one instance During application life cycle like& Nbsp;java.lang.runtime. when to use the Static Class in place of Singleton in javaindeed there is some situations, where Static classes makes sense than Singleton. Prime example of this is java.lang.math which are not Singleton, instead a class with all static methods. Here is few situation where I think using static class over Singleton pattern make sense: 1) If your Singl Eton is isn't maintaining any state, and just providing global access to methods, than consider using static class, as Stati C methods is much faster than Singleton, because of static binding during compile time. But Remember its not advised to maintain state inside static class, especially in concurrent environment, where it could leads Subtle race Conditions when modified parallel by multiple threads without adequate synchronization. you c An also choose to use Static method, if you need to combine bunch of utility method together. Anything else, which requires singles access to some resource, should use Singleton Design pattern. Difference between Singleton vs Static in Javathis are answer of our Second interview question about Singleton ov Er static. As I said earlier, fundamental difference between them is, one represent object and other represent A method. Here is few more differences between static and Singleton in java. 1) static class provides better performance than Singleton pattern, because static methods is bonded on compile time. 2) One more difference between Singleton a nd static is, ability to override. since static methods in Java cannot is overridden, they leads to inflexibility. On the other hand, you can override methods defined in Singleton class by extending it. 3) Static classes is hard to Mock and consequently hard-to-test than singletons, which is pretty easy-to-mock and thus easy-to-test. It ' s easier to write junit Test for singleton than static classes, because can pass mock object Wheneve R Singleton is expected, e.g. into constructor or as method arguments. 4) If your requirements needs to maintain state than singleton pattern is better choice than static class, Becausemaintaining &nbs P;state in later case was nightmare and leads to subtle bugs. 5) Singleton classes can be lazy loaded if its An heavy object, but static class doesn ' t has such advantages and always eagerly loaded. 6) Many dependen CY Injection Framework manages Singleton quite well e.g. Spring, which makes USIng them very easy. these is some differences between static class and Singleton pattern, this would help to Deci De between, which situation arises. In next section we'll when to choose Singleton pattern over Static class in Java. advantage of Singleton Pattern over static Class in Javamain advantage of Singleton-over-Static is, former is more object oriented than Later. With Singleton, you can use inheritance and polymorphism to extend a base class, implement an Interface and capable of providing different implementations. If We talk About java.lang.runtime, which are 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.runt Ime an Static class, it ' s not possible to return different implementation for different jvm. that ' s all on Diffe Rence between Singleton and static CLASS in Java. When you need a class with full OO capability, chose Singleton, and if you just need to store bunch of static methods T Ogether, than use static class. other
Java Design Pattern TutorialsFrom javarevisited Blogwhen to use Builder design pattern in javaa Real Life example of Observer pattern in javahow Decorator pattern in javadifference between Factory and Abstract Factory pattern in Java10 SOLID and Object oriented desi GN Principles Java Programmer should know
Read more:http://javarevisited.blogspot.com/2013/03/ Difference-between-singleton-pattern-vs-static-class-java.html#ixzz3oinuirjt
difference between Singleton Pattern vs Static Class in Java