This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes prefer static methods over non static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "prefer static methods over non static methods" Watch "prefer static methods over non static methods" New topic
Author

prefer static methods over non static methods

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I have a utility class:



and now I use that in (a stateless ejb) class:




Is the use of a static method better than using dependency injection?








For both versions, everything works as expected, but I am unsure about the quality of my code (Performance. Maintenance, ..). What version is really better?



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
What static methods? I can't see any static methods. I can see static nested classes, but that is something different. A static nested class is easier to use than non-static because you don't have to create an instance of the outer class.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

Assuming that you turn them into proper methods, instead of inner classes....

The advantage of the injection approach is that it makes it simple to provide an alternative implementation, whereas with a static method you're tightly coupled to that particular method. So I would say it depends on whether there's any chance you'll want to have a different implementation. In the example you've given, it seems unlikely. But if the utility method was more complicated, and especially if it had dependencies on other components, then being able to mock up an alternative might be very useful for unit testing, for example.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Oh sorry, I wrote it false:

I really mean a static method:



I have corrected it above. But the questions is the same.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
The advantage of the injection approach is that it makes it simple to provide an alternative implementation, whereas with a static method you're tightly coupled to that particular method.


Thank you, this is a really a good point!

Are there any risks or cautions when using static methods within an ejb-container ? I guess, static Methods bypasses some typical ejb-states. (EntityManager, Transaction, Security,..?).


But if the utility method was more complicated, and especially if it had dependencies on other components..


Yes, my static method calls other static methods from other classes. So should I go for dependency injection? I am actually using it, but I have thought to provide static methods as much as I can, is a good design choice.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: prefer static methods over non static methods