On 11/22/10 2:50 PM, Guillaume Nodet wrote:
> AFAIK, the best pattern for that is to use a final with a private
> constructor and declare public static final fields in it.
>
> public class Constants {
> private Constants() {
> // non-instantiable class
> }
>
> public static final String TYPE = "type";
> }
>
> This avoids all the problems as it forces the user to import the
> constants (and not inheriting the interface).
Probably a good idea. Instead of using an Interface, using a class with
public static final is ok.
Note that Joshua Bloch mention the use of Interface being *implemented*.
We don't do that.
Here, it's really just to define some constants, as Java was initially
designed without such a data structure. They added the 'enum' later, but
it's just not enough. Using class with public static final or not
implemented Interface fulfill the same need : to have constats available
all over he code easily.
I would loved having a 'const' qualifier for classes/interfaces, or even
a const type (class, interface, enum and const would have been perfect),
but they didn't added it - even though the 'const' keyword is reserved
for future usage (those idiots never used it, so you have a way too many
semantic for the 'final' keyword, even in method parameters where the
const keyword would have been way more explicit).
You wonder why SUN has been bought by Oracle ? They were just non
consistent enough ...
So, it's up to you : keep the interfaces as they are, or change them to
Class with a private constructor, and declare all the fields as public
static final just fits me.
--
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com
|