Let's take a quick break from APIs to learn about another new concept: Constants. In Java, a Constant is a value that does not change. In other words, it is constant. While simply creating a constant is incredibly easy, it's the manner we use them that can make our programs much more robust. We'll discuss their use case in a moment. First, let's take a look at an example.
There are plenty of constants built into Java. For instance, the
Integer class includes a
MAX_VALUE constant to define the maximum size of an
Integer. We can see this in the REPL:
java> Integer.MAX_VALUE; java.lang.Integer res2 = 2147483647
As you can see, constants are traditionally named using all capital letters. If a variable name has multiple words, they're separated with an underscore.
When we define a constant, it looks like this:
public static final String MY_SCHOOLS_NAME = "Epicodus";
final modifier. As the name suggests,
final means the variable is unchangeable. It's final. Enough said. No method in any class (including its own) has permission to alter this value. This is part of what makes a constant powerful: You can rest assured knowing nothing outside of the developer literally editing the source code itself can change this value. Using constants helps keep large, complex codebases more secure.
It's best practice to scope constants to the context in which they're used. When we refer to scope, we're referring to the area in which a variable (like a constant) is available. For instance, a variable defined within a method is scoped within the method. That is, it's only available within the method. If we tried to call the variable in a different method, Java would not know what we're talking about.
On the other hand, a
static variable defined at the top of a class is scoped to that class. That is, it's available anywhere within that class, it's not "stuck" within a method like the previous example.
As you begin to dig deeper into Java, pay attention to the manner in which you scope content. Items should be declared where they will be used. Scoping content to a larger context than necessary can cause performance issues, because it requires Java to store and provide information to more areas of your application than absolutely necessary.
It's best practice to limit scope whenever possible to keep code robust and performant.
There are several primary approaches to scoping constants in particular:
If a constant is used in multiple classes, they can be defined in their own file or class. Each class in the same package will automatically have access to the class. Or, if you require use of the contents in this file in another package, you may import it manually. This is common in Android Development.
If a constant used by more than one method in a class, they should be defined
static at the top of the class. The
static modifier, as you know, means the variable is defined class-wide.
If a constant is used only within a single method, it should be defined within that method. In this case, it will not be declared
We can use constants strategically to drastically reduce room for error. For instance, imagine your application used the same
String in many different places. Instead of relying on everyone to consistently use the same exact
String in multiple places, developers will often define one constant value, and refer to the name of the constant throughout the code instead. This offers several benefits:
The value will always be consistent, because we're always pointing back to the one single constant.
If the developers later need to alter this constant's value, they can do so in one place, without hunting down every single instance of the
The constant can be used directly in tests as well. Again, developers can alter the constant's value when necessary in a single place, and all tests are still relevant to the code base, without requiring a manual update.
If you misspell the constant variable name, the compiler will alert you. If you misspell a
String's contents, or insert another incorrect value, Java won't know that's not what you meant to do, and the compiler will be unable to alert you of your mistake.
By using constants correctly, we can reinforce our code, and make it much easier to maintain.