How to organize helper functions

Milan picture Milan · Apr 9, 2013 · Viewed 10.5k times · Source

I need to create lot of helper routines for converting strings.

Something like :

String Function1(String s) {}

I would like to call them from any Activity.

What is the best way to do this ? Do I need to create a class or not ? I was thinking just to have one separate file with all these functions. Is this a candidate for a package or not ?

Answer

Ali Behzadian Nejad picture Ali Behzadian Nejad · Apr 9, 2013

Create a class with public static methods, then you can call them every where with ClassName.methodName(parameters):

public class Util {
  public static String methodOne(String param) {
      //do something
      return param;
  }

  public static String methodTwo(String param) {
      //do something
      return param;
  }

  //...
}

Inside other classes:

String someString = Util.methodOne("Some String");
// ...