• Post Reply Bookmark Topic Watch Topic
  • New Topic

interface vs. class with private constructor?  RSS feed

 
Manzar Zaidi
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • 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?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!