Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
}
like image 464
Zon Avatar asked Mar 14 '23 11:03

Zon


1 Answers

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.

like image 138
chiastic-security Avatar answered Apr 07 '23 22:04

chiastic-security