Fortran 2003 Standard

PROTECTED

Purpose

The PROTECTED attribute allows greater control over the modification of module entities. A module procedure can only modify a protected module entity or its subobjects if the same module defines both the procedure and the entity.

Syntax

The PROTECTED attribute must only appear in the specification part of the module.

Read syntax diagramSkip visual syntax diagram>>-PROTECTED--+----+--entity_declaration_list------------------><
              '-::-'
 
entity
A named variable not in a common block.

Rules

If you specify that an object declared by an EQUIVALENCE statement has the PROTECTED attribute, all objects specified in that EQUIVALENCE statement must have the PROTECTED attribute.

A nonpointer object with the PROTECTED attribute accessed through use association, is not definable.

You must not specify the PROTECTED attribute for integer pointers.

A pointer object with the PROTECTED attribute accessed through use association, must not appear as any of the following:

Attributes compatible with the PROTECTED attribute

Examples

In the following example, the values of both age and val can only be modified by subroutines in the module in which they are declared:

      module mod1
          integer, protected :: val
          integer :: age
          protected :: age
          contains
              subroutine set_val(arg)
                  integer arg
                   val = arg
              end subroutine
               subroutine set_age(arg)
                  integer arg
                   age  = arg
              end subroutine
      end module
      program dt_init01
          use mod1
          implicit none
          integer :: value, his_age
          call set_val(88)
          call set_age(38)
          value = val
          his_age = age
          print *, value, his_age
      end program

Related information

Modules

PRIVATE

PUBLIC

End of Fortran 2003 Standard