aspose file tools
The moose likes Beginning Java and the fly likes ConstantInterface vs ConstantClass(Enum) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "ConstantInterface vs ConstantClass(Enum)" Watch "ConstantInterface vs ConstantClass(Enum)" New topic
Author

ConstantInterface vs ConstantClass(Enum)

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Mar 12, 2011
Posts: 808
I say constants do not belong in an interface. And surely this example is better served by an enum?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 4761
    
    7

Dennis Deems wrote:I say constants do not belong in an interface.

Seconded.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

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


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: ConstantInterface vs ConstantClass(Enum)
 
Similar Threads
Bit of Theory regarding Interfaces
interfaces and exceptions
why not use interfaces for constants?
HF - What goes where question
Backtracking recursion problems.