Syntax help with 2D array

Neither static nor final are actually required for any code to run!

But when defining data which shouldn’t be mutated (regardless it’s enforced or not by the compiler), it’s pretty common to pair static final together, as we can see in this excerpt from Processing’s own source code:

Although I’d rename that array to PLATFORM_NAMES, which is the appropriate naming convention for read-only data.

Although Java arrays are mutable, when we see at least 2 of these elements together (static, final, SCREAMING_SNAKE_CASE), those’d be a telltale sign we should only read such data, but never modify it.

Actually, the SCREAMING_SNAKE_CASE naming convention alone means immutability for any programming language.

Keyword final makes the compiler disallow a variable/parameter/field to be re-assigned to another value later:

However, the keyword final in Java doesn’t protect an object from being mutated, only its variable from being re-assigned!

Keyword static, when applied to a field or a “global” variable, it enforces its associated data to be created a single time only, regardless how many times its class might be instantiated later:

Although when using the PDE (Processing’s IDE), it’s not easy to create multiple instances of a sketch running all at once at the same time, it’s still a good convention practice to add static when defining an immutable data, together w/ final and SCREAMING_SNAKE_CASE.

1 Like