• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Bean vs Class with static methods

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
we have a util package that contains a helper class:


the idea is that validator is thread safe, so we don't want to create a new instance in controllers all the time, so we just call static methods on the class.

but this could be achieved by creating normal class, where constructor creates validator instance, and mehtods are not static and used as a bean and autowired in controllers.

application context is loaded only once so we know that validator isntance is created only once on application start.

Is it better to leave it as it is (and why) or use a bean (and why)?
Thanks
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.

I think the difference would be where the object is held. An instance would be on the heap. But created up front, only one, so it would get into the tenure space quickly, which is a good thing.

If it is static methods, then it goes on perm gen and the stack. perm gen usually has less space in it.

I try not to have any static methods myself unless you have an extremely good reason why it has to be static.

Mark
 
reply
    Bookmark Topic Watch Topic
  • New Topic