File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static methods" Watch "Static methods" New topic

Static methods

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Just wondering...
I've been using static methods that call stored procedures from our database, for quite some time. I was just wondering if this is something I should NOT be doing, and if not , why, and what's the correct way?

(I have run into a few problems recently, and was wondering if this was causing my problems.)

Thanks for your input!
Alain Boucher
Ranch Hand

Joined: Feb 25, 2003
Posts: 51
No problem with that. The only thing is the exception catching...

a static block can throw exception but the managing of those exception is not standard... if any error, SQLException for example are thrown, you will receive a java.lang.ExceptionInInitializerError not a exception...

I think the best thing to do is to write a simple normal class that need to be instanciate and then call a init method to create your connection etc... Try to use static variable only for Constant variable. You can use static variable but the process of loading data in those variable can be not static... but who really mind... if it's working it's working...

Alain Boucher<br />Spare-Brain Consultants Inc.<br />SCJ2P,SCWCD,
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I think Alain is talking about static initializers, however Jennifer mentioned static methods.

As for my own answer, I'm fairly new to JDBC, so I don't know all the implications. However, from a more OO point of view, I might balk at using only static methods. Personally, I would need more details to decide whether or not this is a good (or bad) design decision.


Java API Documentation
The Java Tutorial
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Ok, I'll chip in. There is nothing deeply wrong with static methods from a functionality point of view. If you are experiencing problems, it's not the static-ness of the method that is causing them.

Still, it's pretty bad OO design. They are basically global functions. You cannot use standard OO techniques such as inheritance with them. When you're writing unit tests, it is impossible to hand your business code a stub or mock implementation of the data access layer. If you need to plug in a different data access layer - because you're prototyping something, because there's a requirements change but you want to retain the ability to quickly back out - you can't write a second implementation and plug that in.

The correct way is to define an Interface with the DAO methods, write a class implementing that interface, and hand an instance of that class as a collaborator to your business code.

I really have to plug those new-fangled Dependency Injection (aka Inversion of Control or IoC) containers here, such as the Spring bean factory (warning: Spring is huge, don't try to use or even understand all of it at once, all the bits are independently useable). They make this highly object-oriented way of structuring your design really natural and enjoyable. Once the concept "clicks" with you, you'll wonder what masochism drove you to ever do without.

- Peter
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Moving this to the OO, Patterns, UML and Refactoring forum...
[ August 23, 2004: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Static methods
jQuery in Action, 3rd edition