Constants

Things To Do First

Things To Do Later

Constants

When programming, you will often need to use fixed or pre-defined values like the GST and PST rates, PI, limits and sentinel values, and other kinds of literals. It's generally considered bad style to write these values as literals right in the code, because it makes the program harder to read and harder to modify. Instead, we use special variables called constants to store special, fixed values during the run of a program.

A constant is a special variable whose value never changes while a program is running. For example, a program that calculates and displays a customer bill for some window blinds will require the GST and PST rates when the tax amounts are calculated. GST and PST will not change while the program is running, and in fact they could be used over and over again throughout the program. Another example could be when calculating areas and volumes for circles, spheres, and cylinders -- you will require the value of PI in order to perform these calculations.

Referring to values as literals in our code as we have been doing so far is not good programming practice. In a large program, using literals results in a huge waste of time when modifications are necessary. Imagine a large program that uses the GST and PST rates in many lines of code. If the tax rate actually changed some day, the programmer would have to search through thousands of lines of code and replace the old value with the new value. Most editor's have a search-and-replace function, but this is not always feasible. What if we wanted to replace the PST of .08 with a new rate of .05 (wishfull thinking!) but there were other occurences of the literal .08 in our code that had nothing to do with taxes! We could do a search-replace for .08, but we'd have to monitor the operation, ensuring we didn't replace the occurences of .08 that weren't tax rates.

Note
Interesting Note: At the time these notes were written, the GST rate in Ontario was 7%. A number of years later, it was lowered to 6%. So there you go. Edited 2010: And now we have 13% HST, which is combined GST and PST, ugh!!!

For these reasons, constants are more desirable. A constant is defined and initialized with its value almost like a regular variable. You must place the "final" keyword before the data type when defining a variable as a constant:

final double HST_RATE = .13;

Note the following:

In your textbook (section 2.7), they define constants inside the main method. In reality, constants are almost always defined at the top of the class outside the main method, between the public class statement and the main method header. Because this is a different area of your program that has some meaning to Java, we have to add the keywords public static to our constant declarations:

public class Example
{
    public static final double HST_RATE = .13;

    public static void main(String[] args)
    {
        // ...
    }
}

Once you've defined your constant, you simply refer to the constant by name (e.g. HST_RATE) in your code instead of using the literal:

public class Example
{
    public static final double HST_RATE = .13;

    public static void main(String[] args)
    {
        // imagine other code here
        // .....
        double taxAmt = subTotal * HST_RATE;
    }
}


                

If the HST rate changes, you only need to change the one statement that defines the constant!

Note
It is generally considered acceptable to use 1 and 0 as numeric literals in your code, so sometimes you will find that you don't need constants for these. Similarly, you can use the literal for null string ("") instead of defining a constant for it.
Read!
In your textbook at the end of Chapter 2.7, there are listed three good reasons for using constants. Make sure you know these for your next quiz/test!

Exercises

Create a new program and define a constant called COLLEGE_NAME. Give the constant the value "Sheridan College".

In your main() method, add the following statements:

System.out.println("You attend " + COLLEGE_NAME + ".");
COLLEGE_NAME = "Sheridan College Institute of Technology and Advanced Learning";
System.out.println("You should really call it " + COLLEGE_NAME);

What happens when you compile this program? Why?

2. Edit your solutions to programming questions 2.1 to 2.7 at the end of Chapter 2 and be sure to use constants where applicable.

[solutions]