This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes Database access via a static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Database access via a static method" Watch "Database access via a static method" New topic
Author

Database access via a static method

Chris Bicnal
Ranch Hand

Joined: Aug 17, 2005
Posts: 80
    
    1
Guys,

What's the general consensus on database access with a static method (or methods). Is this generally frowned upon?

For the sake of clarity, I'm thinking about some business logic which needs to read something from the database (say for instance whether a person is over a certain age limit).

Would it be bad to encapsulate this logic into a static helper method - and if so, why?

Thanks,

Chris

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

There is too little information there to say for sure if it is good or bad, but it does feel a little icky to me. The problem with static methods for this sort of thing is that they aren't very flexible - it makes it harder to reuse code without creating un-necessary dependencies, harder to mock an object for test, etc...

Since the database can be expected to change independently of the business logic (and the logic independently of the database) I feel it is generally better to isolate one from the other.

You might want or need to subclass the business object, and you might need to override or change the behavior of the way the data is taken: or you may want to generalize the interface to the business logic and how it gets the data it needs. This is all pretty simple to do until you have to think about static methods and shadowing and which version gets called. So for that reason I would rather the data-access be handled in an instance method rather than static (independent of whether it is in the same class as the business logic or not).


Steve
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Database access via a static method