The Coder's Handbook
Constants
USING CONSTANTS
What is a Constant?
Sometimes in a program you'll use a value that is consistent across the entire program and it never changes. For instance, we might want to have a variable represent an approximation of pi. Your code really shouldn't change pi, because it's a fixed value.
To do this, we can use the keyword final to designate that a value is a constant. This means that the value cannot be changed anywhere else in the program.
final double PI = 3.14159265359;
When you use a constant, think of it as a sealed box. Once we initialize it with a value, it cannot be changed.
Why not just use a literal?
A literal is a hardcoded value. For example, we might just print out the number 5 or the letter 'a'. When you have a very important literal that's in your program, it is often referred to as a magic number. Magic numbers are bad. Here are a few reasons why:
Clarity - It's easy to forget the purpose of a number. Perhaps while I am writing the a game, it seems obvious to me that a sniper rifle does 87 damage. But later on, I'll have no idea why the number 87 is randomly found in my damage code.
Ease of Use - It's hard to update. If I decide the sniper rifle is too powerful and reduce its damage to 72, I'll need to find every place in my code that says 87 and update it.
Bugs - It's easy to make a mistake only updating the data in one place but not another. For instance, I might update the text label for the user to say 72, but the weapon still does 87 damage in my damage code.
When given proper names, constants and variables can be considered self-documenting. That means their names describe their purpose, making your code clearer without even needing comments.
Sorry - no magic allowed in Java.
Why not just use a variable?
Let's consider a new example - a very large video game you are working on a large programming team. There are thousands of fixed values about player abilities, classes, and weapons. You might represent some of those values as constants:
final int ROGUE_BASE_HEALTH_PER_LEVEL = 5;
final int FIREBALL_MIN_DAMAGE = 8;
final int FIREBALL_MAX_DAMAGE = 48;
final double CRITICAL_HIT_CHANCE = .05;
If these were these variables, there's nothing stopping anyone on the team from changing those values anywhere in the code. Imagine, for a moment, they are variables.
double criticalHitChance = .05;
What if Kyle the intern writes the following line of code, trying to detect if a user scored a critical hit.
double roll = Math.random(); // Random number between 0 and 1
if(criticalHitChance = roll)
{
doDoubleDamage();
}
Kyle forgot that we use the equality operator (==) to test equivalence rather than the assignment operator (=), and in his code above he accidentally changed the value of criticalHitChance to a random number. Now everything's a mess across the whole program. Way to go, Kyle.
Using constants prevents us from making errors. Even when you're working on a problem alone, you will make mistakes. All of us are Kyle sometimes. So if a value isn't ever meant to change, use a constant.
STYLE
Why All Caps?
In the examples above all the constants are in all capital letters with words split by underscores. We call this MACRO_CASE. This is for the same reason we use camelCase to name variables. It's simply a useful convention. These are industry standards, and it helps make your code easier to read:
If I see something in camelCase, I know it is a variable.
If I see something in MACRO_CASE, I know it is a constant.
If I see something in PascalCase, I know it is a class. (More on this later!)
If your code is well written, a reader should never have to look at the declaration to understand what they're looking at.
STORAGE AND CHANGE
Three Forms of Data
In Java, data can come in three forms: literals, variables, and constants.
Literals are just numbers typed into the program. They only exist for a moment and are not stored, and thus cannot change
Variables are boxes that store values of a given type and tie it to a name. They persist and can have their values changed.
Constants are locked boxes that store a specific value. They persist and cannot be changed.
Literal
Variables
Constants
Properties
No memory
Cannot change
Examples
5
'a'
Properties
Memory
Can change
Examples
int number
char letter
Properties
Stored in memory
Cannot change
Examples
final int NUMBER
final char LETTER
RESOURCES
Alex Lee - Basic Tutorial on Final Keyword
Coding With John - All uses of final (includes objects / classes)