Zon
Zon

Reputation: 19918

Coding conventions: final class instance in CAPITAL_LETTERS?

Constants (final) of primitive types should be written in CAPITAL_LETTERS. But what about a class instance? For example, when it is passed as a function parameter, is called from inner class and should be declared final. Are all parameters supposed to be final? Should it be this way then:

public static void myFunction(
    final MyClass CLASS_INSTANCE) {

    // Code.
}

Upvotes: 4

Views: 1965

Answers (3)

Hoopje
Hoopje

Reputation: 12952

To start with a nitpick: Java does not really have constants. It has final static variables, which -- for all intents and purposes -- often behave like constants. But they behave differently (and unexpectedly) in some rare situations, even when they have a primitive type.

Anyway, by convention, variables which behave like constants are given name in capitals. For example, java.awt.Color defines constants RED and BLUE of type Color. (It also defines constants red and blue, but since RED and BLUE were added later, I suspect the Sun/Oracle people considered those names a mistake.)

But parameters are not constants and do not behave like them. For every method invocation they can have a different value. Thus parameters are always named in camel case, even if they are declared final.

And should parameters be declared final? When, here is where convention stops and mere taste begins. Some people say yes, some people say no. I belong to the "no" camp. Making a parameter final could help prevent introducing bugs by giving a compiler error if you try to modify it. However, if your method body is so long that you actually require this help, then your method should probably be refactored. On the other side, I find parameter lists without final keywords everywhere easier to read and clearer, so I tend to leave them out.

Upvotes: 0

Andy Thomas
Andy Thomas

Reputation: 86509

No, final parameters should not be written in all-uppercase -- they're not constants.

The terms constant and final are not synonymous.

Uppercase is indeed used for constants, as specified by early Java naming conventions.

The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores ("_").

But not all variables declared final are constants. From the Java language specification, section 4.12.4, "Final variables":

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

Parameters are not constants. They're not initialized with a constant expression. And in your example, the parameter is not a primitive type or String.

Therefore, parameters are specified in mixed case, with an initial lowercase first letter.

Upvotes: 2

chiastic-security
chiastic-security

Reputation: 20520

CAPITAL_LETTERS... What about a class instance?

Nope. That would be weird. Parameters use camel case. The fact that something is final doesn't affect conventions around case.

Are all parameters supposed to be final?

No. Declare things final if they shouldn't ever change. That often applies to a parameter, but not always.

Declaring something final does two things: it helps pick up bugs where something never gets initialised or can be changed after initialisation; and it acts as a hint to the compiler to allow some optimisations.

Upvotes: 5

Related Questions