R
Rhino
This question is completely unrelated to the one I just asked about
initializing a Map....
--
I have a class named StringUtils containing several utility methods. The
class is stored in a package called "common" because it could potentially
be utilitized by lots of other classes. The methods in this class include
count(), which determines the number of occurrences of a character in a
string, and blanks(), which creates a String containing the number of
blanks specified in the input parameters (for example, blanks(4)
generates a String containing 4 blanks).
I'm trying to figure out if this utility class should be defined final
and have all of its methods defined as static, just the way things work
with the Math class, so that my classes could be referenced statically.
For example, if the methods were static, I would write my code like this:
int num = StringUtils.count("a", "foo");
System.out.println(StringUtils.blanks(4) + "Hello world");
Or would it be better to have a StringUtils NOT be final and NOT make its
methods static? In that case, I'd have to instantiate StringUtils, then
use the reference to the instance to use the methods, like this:
StringUtils myUtils = new StringUtils();
int num = myUtils.count("a", "foo");
System.out.println(myUtils.blanks(4) + "Hello world");
Either way will work but which is the better design and why?
And no, this is not a homework question. It's just one of many gaps in my
own understanding ;-)
initializing a Map....
--
I have a class named StringUtils containing several utility methods. The
class is stored in a package called "common" because it could potentially
be utilitized by lots of other classes. The methods in this class include
count(), which determines the number of occurrences of a character in a
string, and blanks(), which creates a String containing the number of
blanks specified in the input parameters (for example, blanks(4)
generates a String containing 4 blanks).
I'm trying to figure out if this utility class should be defined final
and have all of its methods defined as static, just the way things work
with the Math class, so that my classes could be referenced statically.
For example, if the methods were static, I would write my code like this:
int num = StringUtils.count("a", "foo");
System.out.println(StringUtils.blanks(4) + "Hello world");
Or would it be better to have a StringUtils NOT be final and NOT make its
methods static? In that case, I'd have to instantiate StringUtils, then
use the reference to the instance to use the methods, like this:
StringUtils myUtils = new StringUtils();
int num = myUtils.count("a", "foo");
System.out.println(myUtils.blanks(4) + "Hello world");
Either way will work but which is the better design and why?
And no, this is not a homework question. It's just one of many gaps in my
own understanding ;-)