Program Logic

Unit 3 Index

Named Constants

Named Constants

Named constants are an efficient way of using fixed data. It is important to realize that the value of a named constant cannot be re-assigned while the program is running. Named constants are typically used when there is a value that will not change during the program execution, however, the value will more than likely be used several times while the program is running.

The Keyword final

Named constants are a good thing and should be used when a value is not going to change while the program is running, but the value could be used multiple times throughout the program. According to our naming standards, named constants must be in all uppercase letters. However, in the eyes of Java, it is the keyword final that actually makes your variable a named constant. If your named constant consist of two or more words, use an underscore between the words for readability. When declaring a named constant, the keyword final is written before the data type of the named constant.

Other programming languages, such as C# have the concept of named constants. The concept is the same, but the value that identifies something as being a constant value may differ.

Declaring a Named Constant

Just like variables, named constants must include a data type. The same data types we learned in the previous chapter can also be applied to a named constant. However, the syntax must also include the keyword that makes the indentifier a named constant. Remember, for Java that keyword is final

final double MAX_SCORE = 100.00;
		

Once Assigned

Remember I said, once assigned, the value of a named constant cannot be changed while the program is running. However, it is possible to declare your named constant and assign the value later. Typically this is done if you need end-user input for the value of your named constant.

final double MAX_SCORE;
//later in the program, but after userInput has a value
//assign that value to MAX_SCORE
MAX_SCORE = userInput;
//once assigned you CANNOT re-assign MAX_SCORE

When to use a Named Constants

I already stated that a named constant is handy when you need an identifier and its value is not going to change, however, the value is likely needed several times in the code.

Say you need to update the current tax rate in a program that used the tax rate value five times throughout the code. If you used a literal such as .055, and the current tax rate changed, you need to change your code in five different places. However, if you used the named constant TAX_RATE, you could change its value in one place and everywhere in the program that used TAX_RATE would automatically be updated. This makes named constants highly efficient, your code is more readable, and your code maintenance easier to accomplish.

What You Learned

What's next?

The next chapter in this unit is The Assignment Operator

Remember, I also recommend you re-visit the learning activity for Naming Rules and Java Keywords.