3

Possible Duplicate:
Interfaces with static fields in java for sharing ‘constants’

We have constants that we need to use in lot of class and my question is if its best practice to create interface with public attribute or something else...

public static final String NAME = "Name";

Thanks!

Community
  • 1
  • 1

4 Answers4

7

You should use a non-instanstiatable class for this, not an interface.

public final class Constants{

  private Constants(){}

  public static final String NAME = "Name";

  // more constants


}

Interfaces should define behavior only, according to Joshua Bloch in Effective Java:

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 causes 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 con- stants, it still must implement the interface to ensure binary compatibility. If a nonfinal class implements a constant interface, all of its subclasses will have their namespaces polluted by the constants in the interface.

Source: Effective Java, Item 19: Use interfaces only to define types

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • Thanks for your answer!do you think that using final class is better than using enum for constants ? Thank you! –  Oct 30 '12 at 11:36
  • @FedorE If you have the possibility to work with enums, that is preferable, but if you need String constants, enums can't provide them. – Sean Patrick Floyd Oct 30 '12 at 13:11
1

Well my suggestion would be to create a class for a constants. Not an interface, if the interface has no methods then it is not serving it's purpose.

Placing constants in an interface was a popular technique in the early days of Java, but now many consider it a distasteful use of interfaces, since interfaces should deal with the services provided by an object, not its data. As well, the constants used by a class are typically an implementation detail, but placing them in an interface promotes them to the public API of the class.

Take a look here.

Paulius Matulionis
  • 23,085
  • 22
  • 103
  • 143
0

You should avoid of using such constants in an interface. You can go with solution proposed by Sean Patrick Floyd. Or use enum type for your constants.

public enum Constants {
 NAME("Name");

 private final String constant; 

 private Constats(String constat) {
   this.constant = constant;
 }

 public String constant(){ 
   return this.constat; 
 }

}

When your code needs them you should use the enum type. Then you will have order in your code.

  • 1
    I would agree with the enum solution, but unfortunately, an enum's member is not a compile-time constant, i.e. it can't be used in an annotation value, for example. Unfortunately there is no compile-time-constant way to get a String from an enum – Sean Patrick Floyd Oct 30 '12 at 10:38
  • You are right. Good that you point that out. I wrote it because it may look that OP or other developer do not need the compile-time constant but only a place where he can store a place holder to String values that is reaused in code. – Damian Leszczyński - Vash Oct 30 '12 at 11:01
0

- First of all answering you question in one breath...Yes its possible, infact if you use

public int x=5; Or

int x=5 ,

this will always be interpreted as public static final int x=5;

- But this is Not the Right way to do it.

- You can better use Enum or Class with Private Constructor to handle this.

Eg:

Class with a static field:

   public class Test{


       public final String NAME = "Name";

       private Test(){}


      }

}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • There's no need to instantiate a class that only holds constants. – Sean Patrick Floyd Oct 30 '12 at 11:07
  • @SeanPatrickFloyd You are absolutely right, But i would like to throw light over this that `"A class with static methods and static fields are equivalent to Sigleton Class", But this should Not be used, as the loading of which class and when by JVM is not a definite process. So Singleton is a safe bet.` – Kumar Vivek Mitra Oct 30 '12 at 11:12
  • Safe but superfluous. Your singleton doesn't even hold the constant field, so it's utterly pointless. Also: it's good practice to make a singleton class final – Sean Patrick Floyd Oct 30 '12 at 11:18
  • @SeanPatrickFloyd i think u missed the constant field NAME in my code... please it properly – Kumar Vivek Mitra Oct 30 '12 at 11:20
  • @SeanPatrickFloyd Moreover if you read it once again carefully, you will come to know that i have also suggested the use of Enum... but provided example of Singleton.... – Kumar Vivek Mitra Oct 30 '12 at 11:21
  • Kumar, I did read your code properly, the thing is: the `NAME` field is static, and hence not a member of your singleton. Of course you *could* access it as `Test.getInstance().NAME`, but any reasonable IDE would warn you that you should change access to a static field to static access, i.e. `Test.NAME`. Your class has a constant and a singleton, but those two are totally unrelated, and therefor the singleton is unnecessary in the context of this question. – Sean Patrick Floyd Oct 30 '12 at 13:09
  • @SeanPatrickFloyd i understood what you meant to say.... thanks for getting the smoke cleared.. I have changed the answer accordingly... – Kumar Vivek Mitra Oct 30 '12 at 17:38
  • @SeanPatrickFloyd and it was nice to meet a guy who has a martial arts background. I have myself practiced Wing-Tsun. – Kumar Vivek Mitra Oct 30 '12 at 17:41