File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes OO, Patterns, UML and Refactoring and the fly likes whats your style with constants? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "whats your style with constants?" Watch "whats your style with constants?" New topic

whats your style with constants?

Sathvathsan Sampath
Ranch Hand

Joined: Oct 03, 2000
Posts: 96
1) Declare constants inside class
class Constants
public static final SOME_CONSTANT = "someVal";

2) Or inside an interface
interface Constants
public static final SOME_CONSTANT = "someVal";

I tend to prefer (2) as I can let my client class implement this. This makes my code less verbose that Constants.SOME_CONSTANT. Thoughts, opinions?
[ June 10, 2005: Message edited by: Sathvathsan Sampath ]

- Sathvathsan Sampath
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
This makes my code less verbose that Constants.SOME_CONSTANT. Thoughts, opinions?

my wo cents:

you can use static imports

java amateur
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1046
Joshua Bloch's "Effective Java Programming Language Guide" has this to say:

ITEM 17: Use interfaces only to define types

When a class implements an interface, the interface serves as a type that can be used to refer to instances of the class. That a class implements an interface should therefore say something about what a client can do with instances of the class. It is inappropriate to define an interface for any other purpose.

One kind of interface that fails this test is the so-called constant interface. ... Classes using these constants implement the interface to avoid the need to qualify the constant names with a class name.

(some code)

The constant interface pattern is a poor use of interfaces. That a class uses some constants internally is an implementation detail. Implementing a constant interface casuses this implementation detail to leak into the class's exported API. It is of no consequence to the users of a class that the class implements a constant interface. In fact, it may even confuse them. Worse, it represents a commitment; if in a future release the class is modified so that it no longer needs to use the constants, it still must implement the interface to ensure binary compatibility. If a non-final class implements a constant interface, all of its subclasses will have their namespaces polluted by the constants in the interface. should export the constants with a noninstantiatable utility class. can minimize the need for unnecessary typing by storing frequently used constants in local variables or static fields, for example:

While I don't see this as a HUGE issue, Mr. Bloch's advice has never steered me wrong. This was written before Java 1.5. As miguel pointed out, static imports will help with the repetitive typing and make the lst point in the quote above obsolete.

[ June 10, 2005: Message edited by: Ryan McGuire ]
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

I place my constants in the class that is most appropriate. Never in an interface.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
subject: whats your style with constants?
It's not a secret anymore!