Comp 110 Constants


Constants are a huge help to keeping your code neat and easy for you to understand. It is difficult to maintain code with having several nameless, hardcoded variables throughout your code. This is where constants come in. Constants are variables whose values cannot be changed.

This is how you declare a variable in Java:

public static final <type> <NAME> = <value>;

The keyword static indicates it is not a field, final means it cannot be changed.

It is conventional to name constants IN_ALL_CAPS separated with underscores. It is a universal flag saying "hey this value can't  be changed once it has been assigned!"