# PROTECTED

Statement and Attribute: Specifies limitations on the use of module entities.

The PROTECTED attribute can be specified in a type declaration statement or a PROTECTED statement, and takes one of the following forms:

Type Declaration Statement:

type, [att-ls, ] PROTECTED [, att-ls] :: entity[, entity] ...

Statement:

PROTECTED [::]entity[, entity] ...

type | Is a data type specifier.
att-ls | Is an optional list of attribute specifiers.
entity | Is the name of an entity in a module.

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

The PROTECTED attribute can only be specified for a procedure pointer or named variable that is not in a common block.

A non-pointer object that has the PROTECTED attribute and is accessed by use association can not appear in a variable definition or as the target in a pointer assignment statement.

A pointer object that has the PROTECTED attribute and is accessed by use association must not appear as any of the following:

- A pointer-object in a NULLIFY statement

- A pointer-object in a pointer assignment statement

- An object in an ALLOCATE or DEALLOCATE statement

- An actual argument in a reference to a procedure if the associated dummy argument is a pointer with the INTENT(OUT) or INTENT(INOUT) attribute.

The following restrictions apply outside of the module in which the entity has been given the PROTECTED attribute:

- A non-pointer entity may not be defined or redefined.

- A pointer entity may not have its association status changed through the pointer.

- A data entity with the TARGET and the PROTECTED attribute that is USE associated may not appear in a pointer initialization expression or a constructor if it corresponds to a pointer component.

## Example

The following example shows a type declaration statement specifying the PROTECTED attribute:

        INTEGER, PROTECTED :: D, E

Consider the following example:

        MODULE counter_mod
          INTEGER, PROTECTED :: current = 0
          CONTAINS

          INTEGER FUNCTION next()
          current = current + 1    ! current can be modified here
          next = current
          RETURN
          END FUNCTION next
        END MODULE counter_mod

        PROGRAM test_counter
        USE counter_mod
        PRINT *, next( )             ! Prints 1
        current = 42                 ! Error: variable is protected
        END PROGRAM test_counter

Parent topic: O to P

## See Also

Modules and Module Procedures
Type Declarations
Compatible attributes
Pointer Assignments