Declaring Constants in Java

Java has the worst syntax for declaring a constant that I’ve ever seen. Here it is so I can remember it. At least in the Java programming culture we have the sense to declare the names in ALL_CAPS.

public class TokenAuthenticationService {
  private static final long EXPIRATION_TIME = 1000 * 60 * 60 * 24; // milliseconds in a day
  // ...
}

This is a good example of high ceremony for a programming language. Just look at all those modifiers. Maybe Java 9 or later can fix this.

Here is a constant declaration in Apple’s Swift. Lack of ALL_CAPS style can be concerning but I suspect that mutability is second class.

class TokenAuthenticationService {
  let expirationTime = 1000 * 60 * 60 * 24; // milliseconds in a day
  // ...
}

Here is the same in Ruby:

class TokenAuthenicationService
  EXPIRATION_TIME = 1000 * 60 * 60 * 24 # milliseconds in a day
  # ...
end