It is very easy to add constant fields to your classes. All you need to do is
add the const keyword to the declaration and set the value. You now have a field
whose value cannot change during the execution of your program. Here is an
example of using const:
This example comes from a class to manage a PID control loop. I learned about
PID control when working with the local robotics team. PID controls allow the
robot program to maintain a constant speed or to turn a turret to a set position.
This is accomplished by using information about how far away from the target
value you are and how fast you are approaching the target value are used to
adjust the output value.
Three constants are used to determine how to adjust the
output value. These constants will be different for each application of PID
control. The constant values for maintaining a constant speed will be different
than the constants used to turn the turret to a set position. If you use the
const keyword for these fields, you will have to create a separate class for
each application of PID control so that you can adjust the constants for each
situation. Using this method, the number of classes could grow very quickly and
become hard to manage.
Read the Rest of this Article at Developer.com