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.
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With