<FINAL Example Program>

Module final_module
  Type t
    Logical :: valid = .False.
    Integer :: code = 0
  Contains
    Final :: zap
  End Type
Contains
  Subroutine zap(x)
    Type(t) :: x
    If (x%valid) Then
      Print *, '[Code',x%code,'zapped.]'
    Else
      Print *, '[Invalid code zapped.]'
    End If
    x%valid = .False.
  End Subroutine
End Module
!
Program show
  Use final_module
  Type(t) :: x = t(.True., 13)
  ! The next line will finalise X (code 13) on the call.
  Call set(x,42)
  ! Nothing is finalised on program termination.
Contains
  Subroutine set(a, code)
    Type(t), Intent(Out) :: a
    Integer, Intent(In) :: code
    Type(t) :: y
    ! The next line will finalise (invalid code) Y.
    y = t(.True., code)
    Print *, 'Setting', code
    ! The next line will finalise (invalid code) A.
    a = y
    ! Returning will finalise Y (code 42).
    Return
  End Subroutine
End Program

■ Execution Results

 [Code 13 zapped.]
 [Invalid code zapped.]
 Setting 42
 [Invalid code zapped.]
 [Code 42 zapped.]