2

One thing is still not clear to me that why people are using interface for storing just constants only ..when the java 5 had come up with it's new functionality of Enums..

    interface OlympicMedal {
  static final String GOLD = "Gold";
  static final String SILVER = "Silver";
  static final String BRONZE = "Bronze";
}

and another class is now implementing it..

public class OlympicAthlete implements OlympicMedal {

  public OlympicAthlete( int aId ){
    //..elided
  }

  //..elided

  public void winEvent( ){
    //the athlete gets a gold medal
    //note the reference is not qualified, as
    //in OlympicMedal.GOLD
    fMedal = GOLD;
  }

  // PRIVATE //
  private String fMedal; //possibly null
} 

As seen above the same thing could be easily establish by enums also , Please let me know if I have choice to store the constants as above which should I choose enums or interface..!

the enum would be like...

public enum OlympicMedal {  

   GOLD, SILVER, BRONZ  

}  

But please advise how the class will access this constants now..!!

Crazy4Java
  • 269
  • 2
  • 6
  • 13

3 Answers3

4

Just use enums. They make your code more type-safe (instead of "stringly typed" as in your example) and are more easily read and maintained by developers used to common Java idioms.

Convenient names

If you need shorter names, static imports will help.

import static mypackage.MyEnum.*;

In your example, the interface using code

package mypackage;

public class OlympicAthlete implements OlympicMedal { ... }

changed to use an enum OlympicMedal instead would be

package mypackage;
import static mypackage.OlympicMedal.*;

public class OlympicAthlete { ... }

Backwards compatibility

If you need Java code that uses enums to work with older class files, just pass -source 1.5 and -target 1.4 to javac to compile the enums down to something that's compatible. This works because enums are just syntactic sugar for regular JS classes. One caveat: some of the support classes (EnumMap, EnumSet) are not available under Java <= 1.4, so -target will not help you getting that code to integrate well.

Community
  • 1
  • 1
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
0

Enum introduced in java 1.5 only. I guess the code you posted might be from older than 1.5

enums are best option because of type safety.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • More likely coders read web articles written before Java 5 was widespread – mmmmmm Aug 20 '12 at 18:09
  • @thinksteep..could you also please update my above code in form of enum and can show me How class is accessing the constants that wil make understanding more clear..!! – Crazy4Java Aug 20 '12 at 18:12
0

why people are using interface for storing just constants only

Bad habbit from earlier versions of Java perhaps?
Use enums instead

public enum OlympicMedal {  

   GOLD, SILVER, BRONZ  

}  

Accessed by another class:

OlympicMedal medal = OlympicMedal.GOLD;

or if you statically import OlympicMedal

OlympicMedal medal = GOLD;

Cratylus
  • 52,998
  • 69
  • 209
  • 339
  • Thanks , could you also please update my above code in form of enum and can show me How class is accessing the constants that wil make understanding more clear..!! – Crazy4Java Aug 20 '12 at 18:12
  • could you also show how the another class will access these constants , that the other thing I want to see..!! – Crazy4Java Aug 21 '12 at 03:34