Difference between revisions of "Named Constants"

From CompSciWiki
Jump to: navigation, search
(updated w/ new content)
m
Line 1: Line 1:
== Named Constants ==
 
 
 
This section will teach you about named constants, including what they are, how to use them, and when to use them.
 
This section will teach you about named constants, including what they are, how to use them, and when to use them.
  
=== What Named Constants Are ===
+
== 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 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 ===
+
== Named Constant Conventions ==
  
 
By convention, named constants are declared just as a regular variable would be, except:
 
By convention, named constants are declared just as a regular variable would be, except:
Line 14: Line 12:
 
* They must have a value assigned to them immediately upon declaration
 
* They must have a value assigned to them immediately upon declaration
  
=== Example of Usage ===
+
== Example of Usage ==
  
 
The program below demonstrates the use of named constants:
 
The program below demonstrates the use of named constants:
Line 33: Line 31:
 
This program contains two named constants: <code>TAX_RATE</code> and <code>PRICE</code>. Note the capitalized name, use of the ''final'' keyword before the type, and the immediate assignment of <code>0.13</code> and <code>10</code>, respectively. Also note that the constants must be declared outside of a method, but within a class body.
 
This program contains two named constants: <code>TAX_RATE</code> and <code>PRICE</code>. Note the capitalized name, use of the ''final'' keyword before the type, and the immediate assignment of <code>0.13</code> and <code>10</code>, respectively. Also note that the constants must be declared outside of a method, but within a class body.
  
=== When to Use Named Constants ===
+
== 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.
 
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 ====
+
=== 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.
 
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.
Line 66: Line 64:
 
Now it is obvious what the items are and which taxes apply to them.
 
Now it is obvious what the items are and which taxes apply to them.
  
==== Declaring Constants as a Product of Other Named Constants ====
+
=== 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:
 
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:

Revision as of 15:20, 20 March 2007

This section 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 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 * (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.

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 * 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.