1

I have made a class that will contain all the utility methods. So, instead of making it as a singleton, I have marked the methods as static and accessing those methods by the ClassName.methodName without the need for instantiation. Is this approach OK?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Piyush Vaish
  • 107
  • 8

1 Answers1

2

Just consider that a singleton is used in order to ensure that only one instance exists for a given class, and that there’s a global access point to that instance.

I believe that having all utility functions marked as static within a class is a good approach since, as you have stated, you will need to use ClassName.methodName in order to use them.

In addition, based on what you want to achieve and the information provided by this link, I would reassert that having a class with static methods is the best alternative.

Community
  • 1
  • 1
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
  • If I dont instantiate, the memory wont be allocated on heap. How is it as compared to the memory allocation while using it the above mentioned way? How does the memory gets allocated if I dont make an instance? – Piyush Vaish Dec 24 '16 at 05:59
  • @PiyushVaish, according to [this](http://stackoverflow.com/questions/7673359/static-method-memory-allocation) static field are stored in a special object on the heap, but you do not have access to it as when you create an instance of a class. – lmiguelvargasf Dec 24 '16 at 13:16