Named Constants

From CompSciWiki
Revision as of 19:47, 4 December 2007 by David (Talk | contribs)

Jump to: navigation, search

COMP 1010 Home > Java Fundamentals


Introduction

Named constants are symbols that represent an unchanged value, which can be used throughout your code wherever that value is required. This allows you to use meaningful names to represent often-used values.

   

{{{Body}}}

What Named Constants Are

Named Constants (also known as final variables or as just constants) are like variables, except once they have a value assigned to them, they cannot be changed. Like regular variables, they can be used as part of an expression.

Named Constant Conventions

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
  • They must have a value assigned to them immediately upon declaration

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, use of the final keyword before the type, and the immediate assignment of 0.13 and 10, respectively. Also note that the constants must be declared outside of a method, but within a class body.

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. This lets you assign a meaningful name to values that never change.

Using 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 value would occur, the name can be used instead. This has the positive effects of making your code self explanatory where named constants are used, and reducing the number of changes you have to make to one if the value changes.

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, PST, and GST, the code is fairly meaningless. Also, if either tax rate changes, you will have to change it in two places. Consider introducting named constants to help explain what is going on in the calculation and to help others understand the code:

  public static final double UMBRELLA_PRICE = 10.00;
  public static final double BREAD_PRICE    = 1.50;
  public static final double LAPTOP_PRICE   = 750.00;
  public static final double MANITOBA_PST   = 0.07;
  public static final double GST            = 0.07;

  ...

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

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

Declaring Constants as a Product of Other Named Constants

Along with simple values, we can assign more complex expressions 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 + PRICE * TAX_RATE;

PRICE and TAX_RATE are available for use in expressions that occur below their declaration, so they can be used to calculate TOTAL.