Search code examples
javaumlclass-diagram

UML Class-Diagramm local variables


Is it necessary to declare all used variables in an Java UML Class-Diagram?

Because if it is, every local- or loop-variable is shown, so the diagram grows huge.

My first thought was to only show the global variables.

Can anybody tell me what is right?


Solution

  • A Class-Diagram shows the methods and variables that are defined in the class context. It does not show "local" variables that are defined in a method's context.

    Example

    Java code:

    class Square
    {
        private double width;
        private double height;
        
        public Square()
        {
            width = 6.0;
            height = 7.0;
        }
    
        public double getArea()
        {
            double a; // local variable -- NOT shown in the class diagram
            a = width * height;
            return a;
        }
    }
    

    Class diagram:

    The variable a is defined in the getArea() method's context and is therefore not shown in the class diagram:

    +--------------------+
    |       Square       |
    +--------------------+
    | -width:double      |
    | -height:double     |
    +--------------------+
    | +Square()          |
    | +getArea():double  |
    +--------------------+
    

    Note: A class diagram does not necessarily has to show all variables and methods. You could hide the private members or the constructor, for example, if you think that its not relevant for the reader.