Hi members, just a simple question, how would I show a constant in an UML diagram?

For example, here is a double field the class contains.
- radius : double

But how would the constant PI (3.14159...) be shown?
- PI : constant??

Future thanks for the responses!

Dani AI

Generated

Short answer: model a constant as a class attribute with a default (initial) value, mark it class‑scope (static) and mark it immutable (changeability=frozen). was right to include type+initial value and was right to add the visibility marker — those are the basic pieces a UML tool expects. (flylib.com)

How to show it (practical): put the item in the attributes compartment and include visibility, type and an initialValue. Indicate class scope by underlining the attribute name (or by a {static} property if your tool prefers). Indicate immutability by adding the changeability/property tag {frozen} (some tools use {readOnly} or a <<constant>> stereotype instead). If you generate Java from the model, that usually maps to a language constant (for example, a class-level final field). (en.wikipedia.org)

Cautions and tips: many UML tools display the default value but a constructor or code generator can override it, so confirm the tool’s codegen rules before relying on the diagram as a single source of truth. If you prefer clearer design, group unrelated constants in a dedicated constants class or use an enum (both are easier to manage in code generation). If the diagram editor cannot draw underlines, use the property string {static, frozen} so the intent remains explicit. (lysator.liu.se)

Code hint (for Java-oriented readers):

public static final double PI = /* initial value */;

This is what most UML-to-Java generators aim to produce when an attribute is classifier-scoped and frozen.

Recommended Answers

All 3 Replies

Its like this

PI : double = 3.14159

EDIT:

Sorry, above poster is correct. I changed my post to avoid confusion.

+PI: double = 3.141...

Oh yep. Syrne is correct. I forgot to put the access modifier.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.