Named Constants

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > Java Fundamentals


Introduction

Named constants are variables that represent a constant value that cannot be changed. This allows you to use meaningful names to represent often-used values. If you find yourself using a literal over and over again, it may be a good idea to change it into a named constant. Not only does this make your code more readable, it is also easier to modify if you need to change that number. See page 68 in the Gaddis text: http://tinyurl.com/gaddis68.


Definition of Named Constants

Named Constants (also known as final variables or as just constants) are like variables except once they have a value assigned to them, they can not be changed. Other than that, named constants can generally be treated like any other variable.


Named Constant Conventions

By convention, named constants are declared just like any other variable 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 void main(String[] args)
  {
    final double TAX_RATE = 0.13;
    final int PRICE ;
    int total ;
    PRICE = 10 ;
    total = PRICE + PRICE * TAX_RATE;
  }
} 

This program contains two named constants: TAX_RATE and PRICE. Note the capitalized name, the use of the final keyword before the type, and the immediate assignment of 0.13. However, note that PRICE is assigned a value outside the declaration. Its value can only be assigned once, but that assignment can be anywhere, as long as it occurs before the value is used.


When to Use Named Constants

Named constants should be used as replacements for any unchanging values that occur in code. Constants 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. Don't be afraid to use constants. They are safer to use than variables since you will be warned if you try to overwrite the stored value and they can make your code (slightly) faster because the compiler knows the value will not be changed. It is also much easier to read the code because the constant's name will convey what the value is for. A case can be made that all identifiers should be declared with "final" except for those where the value must change such as a loop index.


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

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 much more clear what the items are and which taxes apply to them.


More on Declaring 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 or sum 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.


Summary

You should now know how to use named constants. This will help your code legibility by both yourself and others.

Previous Page: Casting Next Page: Review Questions and Exercises