18

I am designing a class diagram for scrabble game. In one of the classes, I have final variable declared. Can anybody tell me, how can I indicate a variable as final in the UML class diagram?

Christophe
  • 68,716
  • 7
  • 72
  • 138
Noopur Phalak
  • 313
  • 1
  • 2
  • 6

3 Answers3

21

There are different notions of final that are all represented in different ways:

  • final definition, i.e. it cannot be overridden in sub-classes - this corresponds to the isLeaf property of the attribute:

    The isLeaf property, when true for a particular RedefinableElement, specifies that it shall have no redefinitions.
    - UML 2.5 specifications, page 99

    There is no official notation anymore for attributes with isLeaf=true; adding {leaf} was the former official notation (UML 1.x) and it is still common.

  • final value, i.e. its value cannot be changed - this corresponds to the isReadOnly property of the attribute:

    If a StructuralFeature is marked with isReadOnly true, then it may not be updated once it has been assigned an initial value. Conversely, when isReadOnly is false (the default), the value may be modified.
    - UML 2.5 specifications, page 106

    Notation for read-only attributes consists of appending {readOnly} to the attribute string.

  • constant usually refers to a non-changeable attribute of the class itself instead of an instance (static final attribute). In UML it would have both properties mentioned above and additionally be static, which corresponds to the isStatic property:

    The isStatic property specifies whether the characteristic relates to the Classifier’s instances considered individually (isStatic=false), or to the Classifier itself (isStatic=true).
    - UML 2.5 specifications, page 105

    Static attributes are indicated by underlining the attribute definition. Constants, as already mentioned are usually UPPERCASE, but that's just a convention.

So, to sum it up, a constant attribute FOO of type String with value "x" would look like this and be underlined in addition (which isn't supported here):

+ FOO : String = "x" {readOnly,leaf}
Christophe
  • 68,716
  • 7
  • 72
  • 138
Carsten
  • 1,248
  • 9
  • 15
14

Constant (i.e. final) fields are indicated via naming convention: constants should be in ALL_CAPS

Source

eboix
  • 5,113
  • 1
  • 27
  • 38
  • 4
    Whilst the naming convention would be a sufficient way of representing a constant, OP also has the opportunity to add a custom stereotype to constant attributes, e.g. «constant» or «final». This will be particularly useful is OP is trying to perform automated analysis on his model, for example using OCL. –  Apr 27 '13 at 17:56
  • 2
    [source](http://cs.nmu.edu/~mkowalcz/cs120f08/uml/UMLdiagram.html) dead :( – Scaramouche Feb 10 '19 at 05:28
  • The source can be found on the [wayback machine](https://web.archive.org/web/20161217072701/http://euclid.nmu.edu/~mkowalcz/cs120f08/uml/UMLdiagram.html) in its state from 2016. However, this is not a valid reference. The answer of [Carsten](https://stackoverflow.com/a/16253663/3723423) with a quote from the UML specs is much more accurate. – Christophe Jul 27 '21 at 11:17
1

Declaring a variable/attribute final is implementation detail. So you don't need to specify it in your CLASS Diagram but you can follow the convention as suggested by eboix.

UML specification doesn't say anything specifically about it; so you can follow convention of showing it in ALL CAPS.

rai.skumar
  • 10,309
  • 6
  • 39
  • 55