4

Always I'm thinking where to put the constants, in interfaces like:

public interface MyInterface {
...
    public static final String MY_CONST = "const";
...
}

Or in classes like:

public class MyClass implements MyInterface {
...
    public static final String MY_CONST = "const";
...
}

What's the better place to define constants?

Jdoe
  • 75
  • 5
  • 1
    Classes. Discussed in Effective Java. – Dave Newton May 27 '12 at 18:09
  • why classes? could you explain it? – Jdoe May 27 '12 at 18:10
  • 2
    http://stackoverflow.com/a/66307/139010 and http://stackoverflow.com/questions/2659593/what-is-the-use-of-interface-constants – Matt Ball May 27 '12 at 18:12
  • This is confusing, people are answering in different ways... – Jdoe May 27 '12 at 18:19
  • 2
    rlegendi does have a point, Jdoe, but the advice to **never ever** use constants in an interface is too strong. Yes, if you put tons of constants in there, especially if you give them some general, nondescript names, it can cause issues. However, starting a new class just to move into it one or two constants with well-chosen names is called *overengineering*. So, as always, be aware of all the consequences and make an informed decision instead of listening to **always** and **never** type of dogma. – Marko Topolnik May 27 '12 at 18:23

5 Answers5

5

The constant interface pattern may be bad practice, but putting a constant in an interface doesn't make it a constant interface. So, if your constant is relevant at the interface level (so relevant to all clients of that interface), go ahead and put the constant into the interface. Nothing wrong with that.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
2

Never use interfaces for defining constants.

For a detailed description, see Robert C. Martin's book Clean Code, or even Josh Bloch's Effective Java. The topic is discussed in details in these works.

The main reason is that they can really confuse someone searching for the value of the constant.

E.g., let's say you have a class A implementing that interface with tons of constants. Then, B also extends that class: you are delegating all the constants to the namespace of the subclass too.

It's a bit like using tons of static imports, which is against readability as well.

rlegendi
  • 10,466
  • 3
  • 38
  • 50
0

An interface could have many implementations. If the value represented by the constants are common to all implementations, being meaningful in the interface context, is it better to declare them in the interfaces.

Otherwise, if the values are specific to some class implementation, and its descendants, will be better to put them in the class.

There are no right or wrong, but contexts more or less appropriated.

jfoliveira
  • 2,138
  • 14
  • 25
0

Another reason for not using an interface: constant values imported from an interface are embedded in the using class. The import relation disappears. When the interface constant gets a different value, the compiler will not necessarily detect that, and recompile the class.

It is a bit unbelievable, and in fact a fixable bug.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

Interface is meant to define contract and not implementation and I see constants as part of implementation because they hold some value for business logic.

So defining constants in a Class make sense. Better explanation and reasoning read this article Define Constants in Interface or Class

Rakesh Prajapati
  • 1,078
  • 8
  • 17