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:



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


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)