Constants

You may use the const keyword to declare a constant. A constant is a variable that must be assigned once only when it is created. After that it is illegal to assign to a constant.

Example 5.3. Constant Example

// Declare some constants
const int ANSWER = 42;
const double PI = 3.142;
const string[] ELEMENTS = ["Earth", "Air", "Fire", "Water"];


// These are all illegal because they attempt to write to constants
ANSWER = 17;
ELEMENTS.resize(5, "Duct Tape");
ELEMENTS[0] = "Muck";


// This is legal
for(int i = 0; i < ELEMENTS.length(); i++)
    print("Element " + (i + 1) + " = " + ELEMENTS[i]);


// Object references can be constant, but there is little point.  You can still
// call methods on the object even if they may alter its state.
const Connection conn = new Connection("Data Source Name");
conn.execute("UPDATE table SET column = " + ANSWER);

// You can't do this since it's a const reference
conn = null;