Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constants, where to place them.

 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hallo,

I hope this is the right forum bacuse I am hit this problem during my devloper research/study phase.
The problem :
In my code I need to use some scalars (String, int , etc) and for this I use the Symbolic Constants and because this contants are use by differnt clients I make them public static and final.Because this symbolic constants are use by more clients with the same interface and a different implementation I move the constants on the common interface.
The question:
Is this an good technique, I must have a constant class ?
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not a good thing to keep all the constants in a single interface. Sometimes the values need to be changed by context.

For example a class Cycle which is inheriting Vehicle can have a constant signalPedestrians which can have value "Bell". A class which is inheriting Vehicle can have value "Honk" for the same constant. Is it clear now?

You can reference both by Cycle.signalPedestrians or Car.signalPedestrians.

Lets think about the other way. If you had Vehicle Implementing movable, you can either declare the constant in Vehicle or interface movable. In both cases you need to give different names and it is tough to manage. Less maintainability
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeep, good tip!
Thanks!
 
peter wooster
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jiju ka:
It is not a good thing to keep all the constants in a single interface. Sometimes the values need to be changed by context.

For example a class Cycle which is inheriting Vehicle can have a constant signalPedestrians which can have value "Bell". A class which is inheriting Vehicle can have value "Honk" for the same constant. Is it clear now?

You can reference both by Cycle.signalPedestrians or Car.signalPedestrians.

Lets think about the other way. If you had Vehicle Implementing movable, you can either declare the constant in Vehicle or interface movable. In both cases you need to give different names and it is tough to manage. Less maintainability


Sorry, but your example doesn't fly, Constants should be defined in interfaces, that's the best place for true constants. Your signalPedestrians sounds like a method to me, it has polymorphic behaviour that depends on the actual type of the object, variables and constants can't do that.

There should be no concept of a "Global" anything in Java, everything is in a class or interface. The constants should be in the interface or class that they are most relevant to. As an example take a look at javax.swing.SwingConstants and javax.swing.Action.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic