• Post Reply Bookmark Topic Watch Topic
  • New Topic

Whats Better? aving all constants in an interfce or having a separate constants file?  RSS feed

 
Gaurav Saxena
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
Which one is the better approach?
a-> Having all the required constants in an interface and making all the classes (that require these constants) implement this interface?
OR
b-> Having a separate constants file in which we declare all the required constants as p.s.f. ?
Please advise!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
551
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurav,
I actually prefer a third choice.

c) Put constants in a separate interface and reference explicitly. For example,
 
Marilyn de Queiroz
Sheriff
Posts: 9082
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recently went to a presentation by Joshua Bloch, principal engineer at Google, previously a distinguished engineer at Sun Microsystems and a Senior Systems Designer at Transarc. He led the design and implementation of numerous Java platform features, including the JDK 5.0 language enhancements and the Java Collections Framework. He is the author of the Jolt Award-winning book Effective Java. He holds a Ph.D. from Carnegie-Mellon University and a B.S. from Columbia.

He strongly recommends that interfaces not be used to export constants for multiple reasons (which I won't list here). Instead he recommends
  • If the constants are strongly tied to an existing class or interface, you should add them to the class or interface. For example Integer and Float MIN_VALUE and MAX_VALUE
  • If the constants are best viewed as members of an enumerated type, use a typesafe enum class
  • Otherwise, you should export the constants with a noninstantiable utility class.

  • "In summary, interfaces should be used only to define types. They should not be used to export constants." -- J. Bloch
    [ August 15, 2004: Message edited by: Marilyn de Queiroz ]
     
    Julian Kennedy
    Ranch Hand
    Posts: 823
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    But soon (J2SE 5.0) we'll have real enums and static imports, giving us the best of both worlds. Hurrah!

    Jules
     
    Tony Morris
    Ranch Hand
    Posts: 1608
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    They are all yuk!

    Put them in a ResourceBundle or a type-safe enumeration (depending on the meaning of the 'constants').

    Hasn't this one been added to a "known anti-patterns" collection?
     
    Gaurav Saxena
    Greenhorn
    Posts: 29
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanx for the advise guys! For my requirements i believe i'll go with a utility class having the desired constants.
    Could have gone for a recource bundle but i think that wont be a good approach as i dont foresee any need of adding or modifying the constants. Not in the near future at least.
     
    Jim Yingst
    Wanderer
    Sheriff
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    [Tony]: They are all yuk!

    I can't tell which post this is in response to, or what it means.
     
    Tony Morris
    Ranch Hand
    Posts: 1608
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Specifically,
    - Constants declared in an interface
    - Constants declared in a "utility" class.
    are yuk.

    By constant, I do not mean the often incorrectly assimilated 'final', but a constant as defined by the JLS 15.28.
     
    Ashik Uzzaman
    Ranch Hand
    Posts: 2373
    Java MySQL Database Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have been using either an interface or an abstract class for exporting final variables/constants. But reading this post, I find J Bloch's claim is quite sensible.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!