Chris Uppal said:
Seems a bit strong to me. If there are some constant values that form part of
some contract, then why /not/ define corresponding constants in the
corresponding Java interface ?
Actually, I'm struggling to think of /any/ reason why defining constants in
interfaces might be considered a bad idea (that doesn't apply to constants in
classes just as well). The only ones I can think of pertain to the
innapropriate use of constants in interfaces, but that's because they are
innapropriate, not because they are constants.
There are two cases here, actually:
1. If the interface is actually used as an interface, then defining
constants there means that those constants will *always* be acessible by
those unqualified names. No one implementing the interface gets a
choice in the matter. That raises some red flags, but might be
acceptable if it's just unthinkable that someone would implement the
interface without using those constants. In most cases, though,
constants of this type are non-type-safe enumerations, and new code
should provide them as a nested enum with 1.5.
2. If the interface only exists to hold constants, then I will stick to
my statement that it's never a good idea. The only reason to do this is
to allow people to implement that interface for the sole purpose of not
needing to qualify constant names. The problem with this is that you've
now made a committment in the external interface of that class;
implementing that interface is a very public thing to do. This is an
abomination. Fortunately, once everyone moves to 1.5, they can use
import static, which is *not* advertized in the public interface of the
class that uses it.
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation