Knowing how to define a Java constant is an essential step to mastering Java. As your programs increase in complexity, the use of constants will help simplify them.
What Is a Constant?
Constants are numbers that do not change, such as the number of hours in a day.
In an algebraic expression, constants are numbers that stand on their own and therefore represent a single unchanging entity.
For example, we call the 7 in 11x - 7 a constant.
Constants vs. variables
A java constant differs from variables in a few key ways.
First, variables are subject to change.
The age of your cat changes from year to year. However, the name, gender, and breed of your cat are constants because they will always stay the same.
Second, while you may use constants to manipulate variables, it never works in reverse.
Use variables to add up sums, determine if something is true or false, allow the user to name things, etc. Define constants to give names to numbers and strings that you use often but never change.
Finally, the ways you define and call on a Java constant are slightly different from how you would for variables.
A Java constant requires the use of modifiers to declare. However, you can easily call on them without having to create an object, as you would with a variable.
Benefits of a constant
A Java constant makes your programs easier for others to read. Rather than remembering why a number is there, they can simply read the names of the constant.
For example, it's easier to understand the meaning of "days_in_April" than it is "30." The number 30 could apply to another month, or relate to something completely different.
Constants can increase the performance of your programs because the Java Virtual Machine (JVM) caches them in addition to your application. That you can call on them directly from a class can also be more efficient.
How to Define a Constant
You can only define a Java constant as a modifier to instance variables. Applying the same modifiers to local variables will not work.
An instance variable is one that exists inside a class, but outside of a method. The following is an example of an instance variable vs. a local one:
public class Name{
>int days_in_April = 30;//Instance variable
void methodName() {
int days_in_April = 30;//local variable
}
}
In order to modify an instance variable so that it becomes a constant, use this line of code:
static final int DAYS_IN_APRIL = 30;
When using a Java constant, ensure that you write it in all caps. This lets you and everyone viewing your application know that it is not a variable.
The words "static" in that line of code is what allows you to call on the constant outside of an object.
By using the word "final," you let the program know that the values will never change, and the JVM can cache them.
Note that "int" is not the only kind of variable that you can modify to make a constant. Although it's is the most common, there is also short, long, byte, char, float, double, and boolean.
Using Constants
Here is an example of how you can put your constant to use:
public class className {
static void main(String[] args) {
System.out.println("Days in April: " + className.DAYS_IN_APRIL);
}
}
This will display the message "Days in April: 30" on the screen.
Add It to Your Toolbelt
Now that you understand how to use a Java constant, how will you implement them in your applications? Let us know in the comments below!
Featured image by Negative Space via pexels.