Static or object for db insert

T

teser3

For the past year I been using Database insert method where I create
an object in my servlet when inserting into my Oracle 9i database.
The insertData method is using PreparedStatement. My insertData method
has closing statements in finally block. There are about 5 records
input in the database per day.

PeopleServlet:

if(myCondition == 0){
new PeopleInfo().insertData(personObject);
}

I was wondering about using static insertData method instead of
creating object. Please advise what the negatives are for the way I am
doing it compared to using a static method to insert database info.
 
T

teser3

If you aren't willing to create an object, then you have to manage all the
individual items piecemeal, and explicitly, in the calling code.  If
'PeopleInfo' is a fairly lightweight holder for those items, the weight of a
holder object is barely more, or perhaps no more than the weight of the
contained items.  The code would be perhaps less encapsulated and extensible.
There don't seem to be many advantages, if any, of the static method approach.
  The instance approach makes for readable, encapsulated code.

What do you fancy will improve if you use a static method?

Thanks Lew, your always very helpful for my Java learning!

I thought static method would be better performance and more accepted
practice in Java because it uses less JVM? And yes PeopleInfo is very
lightweight with few conditions and it (Servlet controller) is part of
my MVC pattern working on Tomcat.
 
T

teser3

If you aren't willing to create an object, then you have to manage all the
individual items piecemeal, and explicitly, in the calling code.  If
'PeopleInfo' is a fairly lightweight holder for those items, the weight of a
holder object is barely more, or perhaps no more than the weight of the
contained items.  The code would be perhaps less encapsulated and extensible.
There don't seem to be many advantages, if any, of the static method approach.
  The instance approach makes for readable, encapsulated code.

What do you fancy will improve if you use a static method?

Thanks Lew, your always very helpful for my Java work!

I thought static method would be better performance and more accepted
practice in Java because it uses less JVM? And yes PeopleInfo is
very
lightweight with few conditions and it (Servlet controller) is part
of
my MVC pattern working on Tomcat.
 
T

teser3

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.


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.

Very helpful information. Thanks for all your time and knowledge.
 
A

Arne Vajhøj

I thought static method would be better performance and more accepted
practice in Java because it uses less JVM? And yes PeopleInfo is
very
lightweight with few conditions and it (Servlet controller) is part
of
my MVC pattern working on Tomcat.

Performance is not very relevant with 5 inserts per day.

Static methods is only accepted in very few cases in Java.

As a general rule: if in doubt between static and non-static
always chose non-static.

I don't think I have ever had to change a non-static method
to a static method.

I will need a pretty big int to calculate the number of times
I have had to go the other way.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top