Search code examples
javaclassmethodsumlclass-diagram

What does it mean when the return type on a UML Diagram is an object?


UML Diagram

The class Chess has a method called knighMoves(int i, int j): Chess with Chess as the return type. Would the method header be

public static Chess knightMoves(int i, int j)
{
   return
}

What would the return statement entail?


Solution

  • Your model tells us that the operation knightMoves() is not static (static members should be underlined in the diagram), and it returns a Chess object.

    You may be confused because the return type is also the enclosing class. But this does not change anything. The java method would look like:

    public Chess knightMoves(int i, int j)
    {
       Chess game;
       … // some code that would assign a value to game
           // and or change its state
       return game; 
    }
    

    or depending on your design intent:

    public Chess knightMoves(int i, int j)
    {
       … // some code that changes the current object
       return this; // return enclosing object
    }