Win a copy of Java by Comparison (eBook) this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Bean vs Class with static methods  RSS feed

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 17314
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
 
no wonder he is so sad, he hasn't seen this tiny ad:
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!