• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

interface vs. class with private constructor?

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At my company we follow this practice of defining all constants in one class.

something like :

public interface MyConstants {
public static final String CONST1="Some value";
public static final int CONST2=456;
}

Nobody here knows why they prefer to use an interface for this and not a Class maybe with a private constructor.
I have seen atleast one book advocating against such use of interfaces, saying that interfaces are just meant to define types.

I dont want to just do whats followed without understanding why.
From your experience, could you tell me pros and cons for using interface vs. class for such a file?
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a side question: why do they put them all in *one* type (be it class or interface)? Do they do this even if the constants, for the sake of example, fall into two logical, but completely unrelated groups?
 
Manzar Zaidi
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeff Albrechtsen:
I have a side question: why do they put them all in *one* type (be it class or interface)? Do they do this even if the constants, for the sake of example, fall into two logical, but completely unrelated groups?


One reason would be to centralise all defined costants. At my company they classify different types by naming styles.
It works well for about 30 constants, but yes, I would also go for different classes based on some classification if the number of contants gets large.
[ January 17, 2006: Message edited by: Manzar Zaidi ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Putting the constants in a class with a private constructor instead of an interface at least helps you to avoid the constant interface antipattern (see this description of static import for some info).
[ January 17, 2006: Message edited by: Jesper de Jong ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic