tags:

views:

1231

answers:

10

Suppose you need to define a class which all it does is hold constants.

public static final String SOME_CONST = "SOME_VALUE";

What is the preferred way of doing this?

  1. Interface
  2. Abstract Class
  3. Final Class

Which one should I use and why?


Clarifications to some answers:

Enums - I'm not going to use enums, I am not enumerating anything, just collecting some constants which are not related to each other in any way.

Interface - I'm not going to set any class as one that implements the interface. Just want to use the interface to call constants like so: ISomeInterface.SOME_CONST.

+1  A: 

Just use final class.

If you want to be able to add other values use an abstract class.

It doesn't make much sense using an interface, an interface is supposed to specify a contract. You just want to declare some constant values.

Megacan
+1  A: 

Aren't enums best choice for these kinds of stuff?

Boris Pavlović
+3  A: 

Enum

gedevan
+3  A: 

Use a final class. for simplicity you may then use a static import to reuse your values in another class

public final class MyValues {
  public static final String VALUE1 = "foo";
  public static final String VALUE2 = "bar";
}

in another class :

import static MyValues.*
//...

if(variable.equals(VALUE1)){
//...
}
Where's the benefit of creating a separate class here? While I normally don't hold the Calendar API up as a good example of design, it's fine in terms of "calendar-related constants are in the Calendar class".
Jon Skeet
the benefit is about not duplicating code, in case you need to reuse constants in more than one class. I guess you can easily see the advantage of this.
Why would you duplicate code? Just refer to the other class. I find it relatively rare that a constant really stands alone.
Jon Skeet
For better readability I'd also have a private default constructor with no body (and a matching comment).
Ran Biron
+2  A: 

My preferred method is not to do that at all. The age of constants pretty much died when Java 5 introduced typesafe enums. And even before then Josh Bloch published a (slightly more wordy) version of that, which worked on Java 1.4 (and earlier).

Unless you need interoperability with some legacy code there's really no reason to use named String/integer constants anymore.

cletus
+1  A: 

Or 4. Put them in the class that contains the logic that uses the constants the most

... sorry, couldn't resist ;-)

Simon Groenewolt
This is not a good idea. This creates dependencies between classes which should have none.
Yuval A
Why not? I'd say that classes that use the same constants have a dependency anyway... And somehow there must be one class that is the most dependent on these constants.
Simon Groenewolt
+7  A: 

Your clarification states: "I'm not going to use enums, I am not enumerating anything, just collecting some constants which are not related to each other in any way."

If the constants aren't related to each other at all, why do you want to collect them together? Put each constant in the class which it's most closely related to.

Jon Skeet
Jon - they are related in the sense they all belong to the same functionality. However, they are not an enumeration of anything... They do not hold the property that an object is "one of those things".
Yuval A
Okay. In that case just put them in the class which is closest to the functionality they're related to.
Jon Skeet
A: 

As Joshua Bloch notes in Effective Java:

  • Interfaces should only be used to define types,
  • abstract classes don't prevent instanciability (they can be subclassed, and even suggest that they are designed to be subclassed).

You can use an Enum if all your constants are related (like planet names), put the constant values in classes they are related to (if you have access to them), or use a non instanciable utility class (define a private default constructor).

class SomeConstants
{
    // Prevents instanciation of myself and my subclasses
    private SomeConstants() {}

    public final static String TOTO = "toto";
    public final static Integer TEN = 10;
    //...
}

Then, as already stated, you can use static imports to use your constants.

Sébastien RoccaSerra
+2  A: 

enums are fine. IIRC, one item in effective Java (2nd Ed) has enum constants enumerating standard options implementing a [Java keyword] interface for any value.

My preference is to use a [Java keyword] interface over a final class for constants. You implicitly get the public static final. Some people will argue that an interface allows bad programmers to implement it, but bad programmers are going to write code that sucks no matter what you do.

Which looks better?

public final class SomeStuff {
     private SomeStuff() {
         throw new Error();
     }
     public static final String SOME_CONST = "Some value or another, I don't know.";
}

Or:

public interface SomeStuff {
     String SOME_CONST = "Some value or another, I don't know.";
}
Tom Hawtin - tackline
A: 

My suggestions (in decreasing order of preference):

1) Don't do it. Create the constants in the actual class where they are most relevant. Having a 'bag of constants' class/interface isn't really following OO best practices.

I, and everyone else, ignore #1 from time to time. If you're going to do that then:

2) final class with private constructor This will at least prevent anyone from abusing your 'bag of constants' by extending/implementing it to get easy access to the constants. (I know you said you wouldn't do this -- but that doesn't mean someone coming along after you won't)

3) interface This will work, but not my preference giving the possible abuse mention in #2.

In general, just because these are constants doesn't mean you should apply normal oo principles to them. If no one but on class cares about a constant - it should be private and in that class. If only tests care about a constant - it should be in a test class, not production code. If a constant is defined in multiple places (not just accidently the same) - refactor to eliminate duplication. And so on - treat them like you would a method.

kenj0418