Home Constants
Constants and magic numbers
Use constants whenever you want to define a single value that won't change during the execution of your program. In particular, whenever you have an arbitrary number or value in your code (a "magic value"), you should use a constant instead.
All constants should take the following form:
public static final int CONSTANT_NAME = 3;
Notice that the constant is always declared public, static, and final, and that the constant name is always comprised of all uppercase letters with underscores separating words.
Whenever you have any sort of arbitrary number or value hard-coded in your code, you should almost always replace them with a constant. We call those hard-coded numbers "magic values" because their intended purpose is mysterious, like magic. Constants allow us to take those mysterious values and give them a name to help make the intent of our code more clear.
Note that constants do not necessarily need to be ints. For example, consider the following:
public static void runCommand(char c) { if (c == 'r') { // ... } else if (c == 's') { // ... } else if (c == 'q') { // ... } else { // ... } }
In this case, it's somewhat unclear what exactly the 'r'
, 's'
,
and 'q'
characters mean. While we might be able to figure this out from
context, it would be nicer to do the following instead:
public static final char RUN_COMMAND = 'r'; public static final char SKIP_COMMAND = 's'; public static final char QUIT_COMMAND = 'q'; // ...snip... public static void runCommand(char c) { if (c == RUN_COMMAND) { // ... } else if (c == SKIP_COMMAND) { // ... } else if (c == QUIT_COMMAND) { // ... } else { // ... } }
All that said, don't go overboard and don't replace literally every single value in your code with a constant, especially if the meaning of the code was already clear before doing so.
For example, consider the following chunk of code:
public static final int FIGURE_SIZE = 10; // ...snip... for (int i = 0; i < FIGURE_SIZE; i++ ) { // ...snip... }
In this case, it makes sense to replace the number 10
in the for loop
with a constant because it clarifies what that number means. However, it would not
make sense to replace the number 0
with a constant. Starting a loop with
zero is a well-known convention in programming, and it's pretty clear what's going on.
As a litmus test, if you find it difficult to come up with a good name for a constant, that's a sign that the variable most likely should not be a constant. For example, don't do this:
public static final int FIGURE_SIZE = 10; public static final int ZERO = 0; // Why would you do this? // ...snip... for (int i = ZERO; i < FIGURE_SIZE; i++ ) { // ...snip... }
Declaring constants
Declare all constants to be public static final
. All constants must
have FULL_UPPERCASE_NAMES
, with each word separated by an underscore.
You should make sure to always remember the static
and final
keywords, and remember to capitalize the name of the constant. That means none of the
following are acceptable:
public static int SOME_CONSTANT = 3; public final int OTHER_CONSTANT = 4; public static final int anotherConstant = 5;
If you are working with an object (for example, when working on the Critters assignment for CSE 142 or when working on any of the homework assignments for CSE 143), you are permitted to also make your constants private if you think it makes sense to do so. However, we encourage you to just keep all your constants public. Since they can't be modified, there's no harm in letting the client view them, and it usually makes sense to allow the client to see what our "default" behavior is likely to be.