Difference between revisions of "Named Constants"

From CompSciWiki
Jump to: navigation, search
m (What Named Constants Are)
(updated w/ new content)
Line 1: Line 1:
This article will teach you about named constants, including what they are, how to use them, and when to use them.
+
== Named Constants ==
  
== What Named Constants Are ==
+
This section will teach you about named constants, including what they are, how to use them, and when to use them.
  
[[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.
+
=== What Named Constants Are ===
  
== How to Use 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 cannot be changed. Like regular variables, they can be used as part of an expression.
  
=== Convention ===
+
=== 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:
 
* The keyword ''final'' must occur before the type
 
* The keyword ''final'' must occur before the type
 
* The name of the constant should be all upper-case letters
 
* 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 ===
 
=== Example of Usage ===
Line 30: Line 31:
 
</pre>
 
</pre>
  
This program contains two named constants: <code>TAX_RATE</code> and <code>PRICE</code>. Note the capitalized name and ''final'' keyword before the type.
+
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.
+
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.
  
=== 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 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.
+
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:
 
For example, the following code calculates tax on three items:
Line 47: Line 48:
 
</code>
 
</code>
  
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:
+
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:
 +
 
 +
<pre>
 +
  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;
 +
 
 +
  ...
  
<code>
 
 
   double total1 = UMBRELLA_PRICE * (MANITOBA_PST + GST);
 
   double total1 = UMBRELLA_PRICE * (MANITOBA_PST + GST);
 
   double total2 = BREAD_PRICE * MANITOBA_PST;
 
   double total2 = BREAD_PRICE * MANITOBA_PST;
 
   double total3 = LAPTOP_PRICE * GST;
 
   double total3 = LAPTOP_PRICE * GST;
</code>
+
</pre>
  
 
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.
  
=== As a Product of Other Named Constants ===
+
==== Declaring Constants 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:
+
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:
  
 
<code>
 
<code>
Line 67: Line 76:
 
</code>
 
</code>
  
Since <code>PRICE</code> and <code>TAX_RATE</code> are declared before <code>TOTAL</code>, they are available for use in any expressions that occur further along.
+
<code>PRICE</code> and <code>TAX_RATE</code> are available for use in expressions that occur below their declaration, so they can be used to calculate <code>TOTAL</code>.
 
+
[[Category:Java]]
+
[[Category:COMP_1010]]
+

Revision as of 15:20, 20 March 2007

Named Constants

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.