*
The moose likes Beginning Java and the fly likes What's the best way to declare global constants? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What Watch "What New topic
Author

What's the best way to declare global constants?

Paul Carter
Ranch Hand

Joined: Sep 20, 2006
Posts: 57
I'm trying to set some values that remain constant and available throughout all classes in the package.

I'm guessing I'll have to put them in a class, but will I have to refer to that class each time I reference them i.e. it would be nice to simply refer to ConstX instead of ConstClass.ConstX (yes, I come from a pampered Delphi background).

Many thanks in advance

Paul.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

In Java 5, there's a new "static import" feature which you'll like very much.

import static com.foo.Bar.CONSTANT;

lets you just refer to CONSTANT anywhere in a source file.


[Jess in Action][AskingGoodQuestions]
Petrus Pelser
Ranch Hand

Joined: Feb 20, 2006
Posts: 132
You could either use enum's or you could define all the constants in an interface and then let your classes implement the interface.
Robert Hill
Ranch Hand

Joined: Feb 24, 2006
Posts: 94
Placing constants in an interface works but is generally considered bad, since it is implementation details which belong in a concrete class.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Paul Carter:
I'm trying to set some values that remain constant and available throughout all classes in the package.

I'm guessing I'll have to put them in a class, but will I have to refer to that class each time I reference them i.e. it would be nice to simply refer to ConstX instead of ConstClass.ConstX (yes, I come from a pampered Delphi background).

Many thanks in advance

Paul.


Well, you can have them in an interface. Like this:



And then any class which wants to use this constants, will implements this interface, now this class doesn't need to refer this constants through interface name.

But in my opinion, don't let implement this interface to each and every class, just refer the constant through interface name if it is needed.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Originally posted by Petrus Pelser:
You could either ... or you could define all the constants in an interface and then let your classes implement the interface.

Originally posted by rathi ji:
Well, you can have them in an interface. Like this: ...
...And then any class which wants to use this constants, will implements this interface, now this class doesn't need to refer this constants through interface name.

No, don't do that! That's called the Constant Interface Anti-Pattern.

Why is that bad? Because it introduces implementation details of a class into the public API of a class.

A well-designed class has a well-thought-through public API (with "public API" I mean the collection of all the public methods etc. in the class). The public API should be independent of how the class is implemented. The implementation should be hidden behind the scenes. Why? Because users of the class don't need to be bothered with exactly how the class does its job, as long as it is doing its job as advertised.

If you implement an interface that contains constants that the class needs for its implementation, then you are including the members of the interface into the public API of the class. So implementation details are leaking to the public API.

Static import was invented just to prevent you from having to use this anti-pattern.
[ October 12, 2006: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Bridget Kennedy
Ranch Hand

Joined: Nov 30, 2004
Posts: 84
Isn't javax.swing.SwingConstants an example of the maligned anti-pattern?

I'm glad this topic came up. A colleague recently implemented this very strategy. It seemed inherently wrong to me to use an interface in this fashion. Then I thought about SwingConstants and figured I was off base.

Always nice when ones instincts turn out to be supported by conventional wisdom.

Thanks!
[ October 12, 2006: Message edited by: Bridget Kennedy ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Bridget Kennedy:
Isn't javax.swing.SwingConstants an example of the maligned anti-pattern?


Yep, sure is. Although many folks consider this bad practice, there are a few otherwise intelligent hold-outs (and handsome ones, too, as well as kind, and also modest) who think the ire is misplaced.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Bridget]: Isn't javax.swing.SwingConstants an example of the maligned anti-pattern?

Sort of, but I think it's not a very good example. The constants defined in the interface are not merely implementation details - they are part of the API, intentionally so. For example, a user of a JButton may need to call button.setHorizontalAlignment(button.CENTER). This, in my opinion, makes it legitimate for the constants to appear as part of the API. Unfortunately, someone using a JButton may be tempted to implement SwingConstants in their own class, just for the convenience of being able to write CENTER rather than button.CENTER or SwingConstants.CENTER. That would be an example of the antipattern, because then the constants would appear as part of the API of the new class, and they are almost certainly not of interest to clients of the new class.

Note I'm not trying to hold up any SwingConstants or JButton as examples of good design. There are numerous other ways this sort of thing could have been done. Nowadays I think enums would be the preferred way to specify the options represented in SwingConstants. I'm just saying that, for all its flaws, SwingConstants is not a good example of the antipattern because the constants are not merely implementation details.

A better example, in my opinion, would be ObjectStreamConstants, implemented by ObjectOutputStream and ObjectInputStream. Here, almost all users of OOS and OIS have no interest at all in these constants - they really are just implementation details within OOS/OIS. Well, there may be a small exception if someone wants to extend these classes, but I think that's an exceedingly rare case. For the vast majority of people, those constants just pollute the APIs of OOS/OIS, making it harder to take in.

To be fair, this antipattern isn't really that big a problem when it occurs, in most cases. Nonetheless, I agree with the common wisdom that it's best to avoid it.


"I'm not back." - Bill Harding, Twister
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Bridget Kennedy:
Isn't javax.swing.SwingConstants an example of the maligned anti-pattern?

I'm glad this topic came up. A colleague recently implemented this very strategy. It seemed inherently wrong to me to use an interface in this fashion. Then I thought about SwingConstants and figured I was off base.

Always nice when ones instincts turn out to be supported by conventional wisdom.

Thanks!

[ October 12, 2006: Message edited by: Bridget Kennedy ]


Yes it is an example of the anti-pattern. You shouldn't push aside your own instincts just because a Sun engineer did it. Like any company not every developer there is perfect and I'm sure they've had their share of those that weren't necessarily very good either. Plus, even gurus make mistakes and there's been plenty of lessons learned. Unfortunately, many of those lessons will be with Java indefinitely, perhaps permanently, for the sake of compatibility. If you need any reassurance of that simply take a look at the number of deprecated APIs out there and the many more that while not deprecated have been replaced with preferable substitutes.
Bridget Kennedy
Ranch Hand

Joined: Nov 30, 2004
Posts: 84
Thanks to all for the excellent feedback!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What's the best way to declare global constants?