classDiagram
class Document {
+String title
-bool published
+publish() bool
}
class Reader
Reader --> Document : reads Loading…
Mermaid 11.15.0
Class diagrams describe static types, members and relationships. Choose a sequence diagram for runtime messages, or a flowchart for decisions and process steps.
Build this in Mermaid Editor →Methods have parentheses; attributes do not. Place a method return type after a space.
classDiagram
class Document {
+String title
-bool published
+publish() bool
}
class Reader
Reader --> Document : reads Loading…
| Syntax | Meaning |
|---|---|
| classDiagram / class Id / class Id["Label"] | Declare a diagram, class id and optional display label. |
| class Id { members } / Id : member | Add attributes and methods inside braces or with colon notation. |
| + / - / # / ~ | Public, private, protected and package/internal visibility. |
| +method(arg) ReturnType / method()* / member$ | Return type follows a space; * marks abstract methods and $ marks static members. |
| List~T~ / Box~List~T~~ | Tildes delimit generic parameters; comma-separated generic arguments are not supported. |
| <|-- / *-- / o-- / --> / -- / ..> / ..|> / .. | Inheritance, composition, aggregation, association, solid link, dependency, realization, dotted link. |
| A "1" --> "0..*" B : label | Quoted multiplicities appear at relation endpoints; a colon adds a relation label. |
| <<interface>> / <<abstract>> / <<service>> / <<enumeration>> | Annotate a class with a stereotype. |
| namespace Name { class A } | Group classes; dotted and nested namespaces are supported in 11.15. |
| note "Text" / note for A "Text" | Add a general note or attach one to a class. |
| direction LR / RL / TB / BT | Set the layout direction. |
| style A fill:#d3e5ff / classDef / cssClass / ::: | Style a class directly or apply a reusable style class. |
| click A href "URL" / click A call callback() | Interactive links and callbacks require suitable host security settings. |
| %% / accTitle: / accDescr: | Comments and accessible descriptions. |
Visibility is a leading symbol. A method is recognized by parentheses; its return type follows the closing parenthesis. Generic brackets use tildes, and generic arguments containing a comma remain unsupported.
classDiagram
class Repository~T~ {
-List~T~ items
#int count
~String scope
+find(id) T
+create() T*
+int total$
} Loading…
Read the endpoint marker: a triangle indicates inheritance or realization; diamonds indicate ownership. Cardinalities are text in quotes, not validated database constraints. Reverse arrow orientation when the relationship needs the marker on the other endpoint.
classDiagram
Base <|-- Derived
Whole *-- Part
Group o-- Member
Reader --> Document
A -- B
Client ..> Service
Contract <|.. Implementation
X .. Y
Reader "1" --> "0..*" Document : reads Loading…
Namespaces group related classes; they are presentation clusters rather than programming language scope validation. Notes can describe a whole diagram or a single class. Keep stereotypes close to the class they qualify.
classDiagram
direction LR
namespace Publishing {
class Store {
<<interface>>
+save() bool
}
class Archive
}
Store <|.. Archive
note for Store "Persistence boundary"
note "Static structure" Loading…
ClassDef defines a visual class; cssClass applies it to a model class. The shorthand ::: applies it while declaring the class. Direct style overrides only the named element.
classDiagram
classDef important fill:#d3e5ff,stroke:#0070f3,color:#171717
class Guide:::important
class Archive
cssClass "Archive" important
style Guide fill:#d3e5ff,stroke:#0070f3,stroke-width:2px
Guide --> Archive Loading…
Hierarchical namespaces render parent clusters by default. Set hierarchicalNamespaces to false for compact, fully qualified namespace labels. This is a layout choice and does not rename class ids.
---
config:
class:
hierarchicalNamespaces: false
---
classDiagram
namespace Docs.Public {
class Guide
}
namespace Docs.Internal {
class Draft
}
Draft ..> Guide Loading…
Close the member block before adding relationships.
Invalid syntax — corrected example below
classDiagram
class Guide {
+String titleclassDiagram
class Guide {
+String title
} Loading…
Use List~T~. Avoid comma-separated generic parameters; they are not supported by this notation.