Re: Static or object for db insert
teser3@hotmail.com wrote:
I thought static method would be better performance and more accepted
practice in Java because it uses less JVM?
It doesn't use "less JVM" to use a static method, necessarily, and static
methods are no more "accepted practice" than instance methods.
There are use cases for static methods, of course. The best practice is to do
the right thing for the algorithm at hand; you cannot simply say that static
methods are better than instance methods or vice versa.
Most of the time instance methods are better. That's because most of the time
you want actions to be controlled through an object, and not globally by a
class. The object that owns an instance method can carry state and control
its behavior independently of other instances. Concurrency is usually easier
to handle with instance methods also.
Notice the words "most of the time", "can carry", "usually easier". When in
doubt, you probably want an instance method, but when behavior must inhere at
the class level then you must use a static method.
Global utility methods, such as the static Math functions (min(), cos(),
etc.), are good candidates for static methods. Factory methods, those that
actually create class instances, will usually be static. Class-wide
behaviors, like registering instances with a class-level registry, have to be
static. When the method has to be static, you should have no doubt.
Otherwise suspect that the method should be instance-level.
And yes PeopleInfo is very
lightweight with few conditions and it (Servlet controller) is part
of
my MVC pattern working on Tomcat.
I think in your particular case that you should stay with an instance method.
The PeopleInfo instance controls it nicely and holds related state in a way
that static variables probably would do less well. Without an SSCCE it's a
little hard to say for sure, so analyze thoroughly.
--
Lew