0

Java Code conventions gives an instruction that constant name must be uppercase.

Java Specification gives a definition of constant:

A constant variable is a final variable of primitive type or type String that is initialized with a constant expression.

Now look, for example, class LocalDate. We can see variables that are not constants by definition, but named as constant (uppercase). Why?

public static final LocalDate MIN = LocalDate.of(Year.MIN_VALUE, 1, 1);
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • [Should a “static final Logger” be declared in UPPER-CASE?](https://stackoverflow.com/q/1417190/12323248), [What is the difference between constant variables and final variables in java?](https://stackoverflow.com/q/30545594/12323248) – akuzminykh Feb 09 '21 at 07:35
  • @akuzminykh it doesn't answer my question – Maria Pomazkina-Karpikova Feb 09 '21 at 11:47
  • Are you sure? The questions seem very similar to me and the answers talk about the same "problem". Are you specifically asking why a JDK developer has done this in this case? – akuzminykh Feb 09 '21 at 16:36
  • @akuzminykh Yes. Both. They are cool and respectable guys, their code is kinda a role model. And their code doesn't follow this easy rule. Wether rule is oldish or they just missed? As a matter of fact Vadim Kotlov answered my question my closing it. He said that my question is opinion-based. It means that only LocalDate developers can answer) – Maria Pomazkina-Karpikova Feb 10 '21 at 04:31

1 Answers1

2

I don't know why the specification makes a difference beween theses types of objects.

Conventions are all about excpectations. My expectation of a uppercase named object is, that it can not change. Also the literal meaning of constant is a situation or object that does not change. Therefore I would use the uppercase naming convention for all immutable static final objects.

According to Oracle, LocalDate is an immutable date-time object that represents a date, often viewed as year-month-day. See https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html.

So I think it is OK to give MIN an uppercase name, because it matches my expectation, that it will never change.

seism0saurus
  • 396
  • 2
  • 10