• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static methods

 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the OO, Patterns, UML and Refactoring forum...
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic