File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Static methods" Watch "Static methods" New topic
Author

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.

Layne


Java API Documentation
The Java Tutorial
Peter den Haan
author
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
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Moving this to the OO, Patterns, UML and Refactoring forum...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

if any error, SQLException for example are thrown, you will receive a java.lang.ExceptionInInitializerError


This is incorrect. The compiler will force you to declare to handle the checked exception (java.sql.SQLException). An ExceptionInInitializerError will occur if a non-checked exception occurs during static initializer execution.

Is there something wrong with using static methods? That question is too broad to have a definitive answer. It might be that you should be using a more object-oriented approach, simply because it is generally accepted as "not nice" in a lot of circumstances. It might be that because you have static methods, your data is shared across multiple threads and so you are experiencing a race condition or deadlock - one can only speculate.

Do you have a more specific question?


Tony Morris
Java Q&A (FAQ, Trivia)
 
GeeCON Prague 2014
 
subject: Static methods