I am programming in Java and was told to refactor some code to coding standards. I was told that util packages are meant for static utility classes and not application functionality. Is this correct? What exactly does this mean? Also, considering the "java.util" package contains Collection interface, how else could I accomplish tasks requiring lists?
-
Um if you were told exactly this what you were told was wrong. Can you clarify that's exactly what you were told? *"Don't use java.util classes except in a static utility class?"* That seems a bizarre assertion. – Radiodef Mar 06 '14 at 21:05
-
Go back and understand what "refactor" means and look at the "coding standards" you are being asked to transform some code into. Focus on your coding standards first and figure out how the "code" does not match the standards. Then look to transform (refactor) to match – ErstwhileIII Mar 06 '14 at 21:06
-
3To be clear though, you shouldn't be putting _your own code_ into java.util… – Joshua Taylor Mar 06 '14 at 21:08
-
Maybe the util package in the app you're working on follows this convention, but this isn't globally applicable. – Nate Mar 06 '14 at 21:09
-
the only real conventions as far as Java packages go is that there are no conventions – matt b Mar 06 '14 at 21:55
-
@Radiodef "One obvious issue was the util package. Util packages are usually meant for static utility classes, not for application functionality." is the exact quote. – Andrew Campbell Mar 06 '14 at 22:32
2 Answers
Whoever told you that was wrong — or you misunderstood. java.util.*
has a bunch of extremely useful, extremely common classes, most of which are not utility classes (in the common sense of "a class with only/mostly static methods for common tasks"). For instance, List
is in java.util
, along with its commonly-used implements ArrayList
and LinkedList
. Ditto Map
/HashMap
/TreeMap
, Set
/HashSet
/TreeSet
, and many others.

- 42,327
- 7
- 87
- 124
-
1I very well could have misunderstood. Either way, at least I now know to discuss further and get clarification. I just wanted to make sure this wasn't a huge rule that I am missing. Thanks! – Andrew Campbell Mar 06 '14 at 22:43
There are no hard and fast rules about what would appear in a util package. To quote the javadoc java.util
contains
the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-number generator, and a bit array).
As you rightly say this is much more that only utility classes. In this case the person seems to be taking the word utility and package util a bit too literally.
I would say that a util package is for miscellaneous useful classes that don't have enough similar classes to justify grouping them in their own package.

- 28,783
- 8
- 63
- 92