简体   繁体   中英

Is it correct to initialize a final attribute with a constructor?

I have to declare an attribute which once initialized can't be modified. It's pretty clear to me that we are talking about a final type, buy my question is whether I can initialize it with the constructor.

Bonus: how should I represent this type of attributes on a UML diagram?

Thanks folks!

When you define an attribute as being final it's not that you could, you must either initialize it inline or through the class's constructor. For names of the attributes that represent constants you should use UPPER_CASE_WITH_UNDERSORES and you should represent them in a UML diagram something like this: + MY_CUSTOM_CONSTANT: Integer = 10 (for a public final Integer MY_CUSTOM_CONSTANT = 10; )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM