Lesson Weekend

Let's begin with a relatively straightforward 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.

Defining Constants

When we define a constant, it looks like this:

public static final String MY_SCHOOLS_NAME = "Epicodus";

final Modifier

Notice the final modifier. We haven't used this one before. 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 code itself can change this value.

Scoping

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:

Application-Wide Constants

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.

Class-Wide Constants

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.

Method Constants

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

Benefits of Constants

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

  • 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 aString'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, much easier to maintain. In the next lesson, we'll add constants to our virtual pets application.

Terminology


  • Constant: A value that does not change, used to reduce room for error. Instead of manually using the same string in multiple locations, for instance, developers will continually refer back to one constant. This ensures the string value will always be the same.

  • Final: When a variable is declared as final it means the variable is unchangeable. That is, no method in this class, or another has permission to alter the variable's corresponding value.

  • Scope: The area a variable or method is available. For example, variable defined in a method is scoped within the method. That is, it's only available within the method. A static variable defined at the top of a class is scoped to that class, and is available anywhere in the class.

Overview


  • Constants should be declared where they will be used. You should limit the scope whenever possible. For instance, if they're only ever used within a specific method, they should be defined within that method. If they're used class-wide, they may be declared static and reside at the top of the class.

  • Constant names are traditionally capitalized, like this:

public static final String MY_SCHOOLS_NAME = "Epicodus";

Additional Resources


  • You can see the other types of constants built into Java in Oracle's documentation here.