Named Constants

From CompSciWiki
Revision as of 16:26, 6 March 2007 by Umhieb34 (Talk | contribs)

Jump to: navigation, search

This article will teach you about named constants, including what they are, how to use them, and when to use them.

What Named Constants Are

Named Constants (also known as final variables or as just constants) are like variables, except they cannot change from their original values. Like regular variables, they can be used as part of an expression, though it is an error to attempt to change their value after they have been declared. Since they represent

How to Use Named Constants

Convention

By convention, named constants are declared just as a regular variable would be, except:

  • The keyword final must occur before the type
  • The name of the constant should be all upper-case letters

Example of Usage

The program below demonstrates the use of named constants:

public class Test
{
  public static final double TAX_RATE = 0.13;
  public static final int PRICE = 10;

  public static void main(String[] args)
  {
    int total = PRICE + PRICE * TAX_RATE;
  }
}

This program contains two named constants: TAX_RATE and PRICE. Note the capitalized name and final keyword before the type.

When to Use Named Constants

Named constants should be used as replacements for any unchanging values that occur in the code. They can also be used to represent the result of an expression containing other named constants.

To Improve Meaning

When you find yourself typing the same value over and over again, consider using a named constant in place of all occurances of that value. So, in all places where the constant value would occur, the name can be used instead. This has the positive side effect of making your code self explanatory where named constants are used, and reducing the number of changes you have to make if the value changes to one.

For example, the following code calculates tax on three items:

 double total1 = 10.00 * (0.07 + 0.06);
 double total2 = 1.50 * (0.07);
 double total3 = 750.00 * 0.06;

Unless you are familar with the prices of the items above and Manitoba tax rates, the code is fairly meaningless. Also, if either the tax rate changes, you will have to change it in two places. Consider using named constants to help explain what is going on in the calculation and to ease maintenance:

 double total1 = UMBRELLA_PRICE * (MANITOBA_PST + GST);
 double total2 = BREAD_PRICE * MANITOBA_PST;
 double total3 = LAPTOP_PRICE * GST;

Now it is obvious what the items are and which taxes apply to them.

As a Product of Other Named Constants

Along with simple values, more complex expressions can be assigned to a named constant as well. For example, to create a constant that is the product of two others, simply do the following:

 public static final double TAX_RATE = 0.13;
 public static final int PRICE = 10;
 public static final double TOTAL = PRICE * TAX_RATE;

Since PRICE and TAX_RATE are declared before TOTAL, they are available for use in any expressions that occur further along.