permaculture playing cards*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Constants, where to place them. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Constants, where to place them." Watch "Constants, where to place them." New topic
Author

Constants, where to place them.

Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

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 ?


SCJP, SCJD, SCWCD, OCPJBCD
jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 306
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

Joined: Sep 18, 2003
Posts: 916

Yeep, good tip!
Thanks!
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constants, where to place them.