This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

"static class" Vs "Singleton"

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
should i be using static class or a singleton , and which is applicable where???
it would be a gr8 help , if some one could discuss "static class" Vs "Singleton" (pros and cons).


thanks again.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A static class might be good for utility methods, especially if you can't move that behaviour to a real object (because you don't have the source for the classes).

Instead of using a Singleton, you should consider the Just Create One pattern: http://www.butunclebob.com/ArticleS.UncleBob.SingletonVsJustCreateOne
 
author & internet detective
Posts: 39789
797
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jigar,
Since you asked this in the testing, I'll point out that static methods are often harder to test. If the static method calls a database or another system (or anything else you may not want happening under test), callers to the method can't mock it out. For this reason, I prefer a singleton with an interface.
 
it's a teeny, tiny, wafer thin ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!