-4

I use Interface for declaring Constants as they are public static final. Lot of times I've seen people write a class with public static final constant variables and sometimes I've done the same. Which one is the better practice to implement, the Interface or using a class?

public class MyCodeConstants {
    public static final String VALUE_1 = "VALUE1";
    public static final String VALUE_2= "VALUE2";
}

or

public interface MyCodeConstants {
    public static final String VALUE_1 = "VALUE1";
    public static final String VALUE_2= "VALUE2";
}

WHICH ONE IS A BETTER PRACTICE TO USE AND WHY??

GraphicsMuncher
  • 4,583
  • 4
  • 35
  • 50
Jitesh Upadhyay
  • 5,244
  • 2
  • 25
  • 43
  • why would you want constants defined in the interface? It seems like exposing the mechanics of the system in a interface as an unnecessary detail. – T McKeown Feb 03 '14 at 03:01
  • Do you *really* want clients implementing that interface? Using `interface` for the side-effect of enforcing `public static final` is not a good enough reason. (really, use an `enum`) – roippi Feb 03 '14 at 03:03
  • @ T McKeown i wants to know that which one is a better practice and the reason to do that. even if i will make constants in class will it not be the same as "exposing the mechanics of the system as an unnecessary detail. " – Jitesh Upadhyay Feb 03 '14 at 03:05
  • If the _only_ purpose of this class is to let clients use the constants, and you really don't want any objects of the class (or subclass), make it a `public final class` and add a `private` no-argument constructor so that nobody can inadvertently extend, implement, or instantiate the class. – ajb Feb 03 '14 at 03:15
  • @mattingly890 please read the question carefully and if you do not like or love the question i can delete :) – Jitesh Upadhyay Feb 03 '14 at 03:41

2 Answers2

1

First, constants should only be specified that manner if they do need to be part of the public API, and you should always consider whether an enum would make more sense.

However, if it really does make sense to have public constants (intent names for Android, the HTTP status codes), then they should go on the type they belong to. In the case of HTTP status codes, they're most closely associated with HttpServletResponse, which is an interface, and so they go there. If the constants are associated with a class (and it's not a better design to pull an interface out of that class), they should go there.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
1

In terms of design both are bad. A class should have methods in your software in order to respect the definition of a class and the encapsulation principle. Interfaces are used to indicate similar behavior of classes and shouldn't be used to stock constants. However, a good solution would be to create a new class that is called Value. You can find more details here :

Should a collection of constants be placed in a class or interface?

Community
  • 1
  • 1
AAmine
  • 100
  • 9