• Post Reply Bookmark Topic Watch Topic
  • New Topic

Symbolic constants, where to place them ?  RSS feed

 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hallo,
I have a few classes with a common interface(all implement the same interce) and in all the classes I use the same symbolic constants.
For the moment the costants are on the common interface but this make the next (interface)implemetation more rigid - all have the same copnstants, the constants can not be redefined.
Other choice is to define an abstract super class class where to define the constants or more simple a simple constant class, but in both cases I'll have empty classes - no methods, only constants and this is not so object oriented.
I took a look on javax.swing.SwingConstants - this is an empty interface, no methods only contants, this seamns to be until now the mosst relayed solution.


So what do you all think ?
Regards, Mihai
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We use a single class containing all our constants.
Another class contains things which are constant for a particular installation but can differ between installations. These are read from a properties file at startup, the rest of the application perceives them as constant.
 
Reghu Ram Thanumalayan
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mihai,
I think is perfectly logical to group constants in a common interface or class even though there are no methods in that.
Even though it may not fit into the definition of object oriented, its more better since it will be easier to maintain the constants in a common place rather than have them scattered around in many classes.
 
Fintan Conway
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mihai,

If you are using the latest version of Java (Java 5), you can replace these interfaces with enums (Enumerated Types - just added to the language).

Enums should give you better flexibility.

Regards,

Fintan
 
Geoffrey Falk
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mihai,

you should create a class to hold your constants (not an interface). The reason is just as you stated: inheriting from an interface or superclass makes your API more rigid.

The following approach is recommended:



See Unit 17 of the book "Effective Java" by Joshua Bloch for a good discussion of this.

Geoffrey

[ March 16, 2005: Message edited by: Geoffrey Falk ]

[ March 16, 2005: Message edited by: Geoffrey Falk ]
[ March 16, 2005: Message edited by: Geoffrey Falk ]
 
Herb Schildt
Author
Ranch Hand
Posts: 253
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mihail:

Fintan's post above is right. The modern way to handle constants is by using an enumeration. Of course, this only works if the values associated with the constants aren't important. For example, if you want constants called Red, Green, and Blue and it doesn't matter what the actual values associated with these constants are as long as the three values differ, then an enumeration makes sense. However, if you have constants that represent specific values, such in Geoffrey's post above, then an enumeration is not particularly applicable.
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frist, thanks for the aswers, now I think I have a picure.
Unfortuntely the requirements are strict java2 1.4 so I must leave with this.
I choose to use a constant class instead of interface.Falk you 're right using a constant interface can make your design more rigid.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!