views:

80

answers:

5

As asked in title, can I make a 'common' public Class and some public static foo() in it? Thanks!

+3  A: 

short and easy: yes.

(but why haven't you simply tried it?)

oezi
Thanks! I've tried it and it works. However, I just want to find other possibilities to fullfill it. Following answers gave me some clues.
herbertD
+2  A: 

A common way is to declare a utility class like this

package my.package;

public class Utility {

  private Utility() {
  }

  public static void foo() {
  }

}

If this class is on your classpath you always can do from everywhere

my.package.Utility.foo();
PeterMmm
Thanks! This is exactly what I've done.
herbertD
+2  A: 

Yes, you can.

Where exactly do you see difficulties?

You can see the popular utility classes (such as StringUtils) in Jakarta Commons Lang for an example. Chances are that some of the very common function you are thinking of are already present in one of the Commons family of libraries.

Thilo
As In OO programming, Object is the first thing we should manipulate, so what I am concerning is: is it 'ok' to use a `public static function()` without instantiate a real object? Thanks a lot!
herbertD
Yes, it is okay. OO is not appropriate for everything. Or rather, in the case of StringUtils, these methods should probably have been methods on java.lang.String, but since they are not there, the best you can do is a static method somewhere else.
Thilo
You've showed me something new, thanks.
herbertD
+3  A: 

yes...You can do it..

rgksugan
A: 

If you have the slightest doubt that your requirements may change, that you might need different versions of some methods for testing etc., then use an interface and a class, e.g.:

public interface Utils {
  public final static Utils INSTANCE = new MyUtils();
  Foo makeFoo();
  Bar transform(Foo foo) 
}

public class MyUtils implements Utils {
  public Foo makeFoo() { return new Foo(); }
  public Bar transform(Foo foo) { return new Bar(foo); } 
}

This makes it easy to switch to other implementations. And you will have less problems when you decide to start using a DI framework like Guice:

public class Client {
  private final Utils util;

  @Inject
  Client(Utils utils) {  //<--- Here Guice will inject for you what you specified
    this.utils = utils; 
  }  
}
Landei
Dependency injection is whole new idea for me, and it too advanced to solve my problem. ^_^. Thanks!
herbertD
My suggestion is independent from DI, I included it just as another reason why you should avoid utility classes when you are not sure. Without DI, you can simply write `Utils.INSTANCE.makeFoo()` instead of `Utils.makeFoo()`, and it gives you the same functionality as a normal utility class, just with more flexibility.
Landei
Excuse me, you mean "without DI, I can use Utils.INSTANCE.makeFoo() that is simpler? I think Utils.makeFoo() is simpler. Thanks a lot!
herbertD
I didn't say it is simpler, I said it is more flexible: easier testable, easy to refactor if you need different implementations etc.
Landei