I understand that instance-level final
variables follow the camel case naming conventions but I was wondering if it should be the case for a Singleton Class as well.
Would you treat final
in a Singleton Class as a constant and follow the constants naming convention as follows:
private final SomeObject SOME_OBJECT;
OR, would you name it in camel case, following the normal variable naming conventions?
private final SomeObject someObject;
This keeps on popping up in multiple code reviews and I always have some grey area. Appreciate any thoughts on this.
According to typical Java coding standards and conventions, the ALL_CAPS identifier style is reserved for static final
constants (and enum
constants ...). In your case, the variable is final
but not static
, so that exception to the normal rules for variables does not apply.
That is my interpretation, and (I think) the most common interpretation. It is not the only interpretation. You and your team could choose to interpret the conventions differently, or even ignore them entirely1.
The most important thing is to be consistent across you / your team / your organization's common code base.
1 - ... though the latter would be unwise, IMO,