Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ConstantInterface vs ConstantClass(Enum)

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know there are many recommendation for Constant class. but some time it is convenient to declare constant in interface. for example say,

I have a specification like below:


there may be x, y, ..z client who may create extra return like ERROR -1 blah, blah(they may create extra constant class)..... but some may required exactly either ON or OFF. why i should allow this client to create one more file for constant. I could have create one more constant file in my API and serve o client but why the extra file? and client have to read the file also..

what you guys say?
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I say constants do not belong in an interface. And surely this example is better served by an enum?
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Deems wrote:I say constants do not belong in an interface.

Seconded.

Winston
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thirded. I really prefer classes like java.sql.Types over interfaces like javax.swing.SwingConstants. Just look at the many classes that implement the latter
 
Campbell Ritchie
Sheriff
Pie
Posts: 48944
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It used to be thought it was a good idea to have constants in interfaces, but that caused problems when a class implemented that interface. Those constants became part of the public interface of all those classes. More recent thinking has it that those constants can be put in a class and imported into any classes needing them. You will find a bit more here, including a reference to Joshua Bloch’s book.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic