PUBLIC

Statement and Attribute: Specifies that entities in a module can be accessed from outside the module (by specifying a USE statement).

Syntax

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

Type Declaration Statement:

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

Statement:

PUBLIC [[::] entity [, entity] ...]

type

Is a data type specifier.

att-ls

Is an optional list of attribute specifiers.

entity

Is one of the following:

  • A variable name

  • A procedure name

  • A derived type name

  • A named constant

  • A namelist group name

In statement form, an entity can also be a generic identifier (a generic name, defined operator, or defined assignment).

Description

The PUBLIC attribute can only appear in the scoping unit of a module.

Only one PUBLIC statement without an entity list is permitted in the scoping unit of a module; it sets the default accessibility of all entities in the module.

If no PRIVATE statements are specified in a module, the default is PUBLIC accessibility.

If a derived type is declared PUBLIC in a module, but its components are declared PRIVATE, any scoping unit accessing the module though use association (or host association) can access the derived-type definition, but not its components.

If a module procedure has a dummy argument or a function result of a type that has PRIVATE accessibility, the module procedure must have PRIVATE accessibility. If the module has a generic identifier, it must also be declared PRIVATE.

If a procedure has a generic identifier, the accessibility of the procedure's specific name is independent of the accessibility of its generic identifier. One can be declared PRIVATE and the other PUBLIC.

Example

The following examples show type declaration statements specifying the PUBLIC and PRIVATE attributes:

REAL, PRIVATE :: A, B, C

INTEGER, PUBLIC :: LOCAL_SUMS

The following is an example of the PUBLIC and PRIVATE statements:

MODULE SOME_DATA

REAL ALL_B

PUBLIC ALL_B

TYPE RESTRICTED_DATA

REAL LOCAL_C

DIMENSION LOCAL_C(50)

END TYPE RESTRICTED_DATA

PRIVATE RESTRICTED_DATA

END MODULE

The following example shows a PUBLIC type with PRIVATE components:

MODULE MATTER

TYPE ELEMENTS

PRIVATE

INTEGER C, D

END TYPE

...

END MODULE MATTER

In this case, components C and D are private to type ELEMENTS, but type ELEMENTS is not private to MODULE MATTER. Any program unit that uses the module MATTER, can declare variables of type ELEMENTS, and pass as arguments values of type ELEMENTS.

The following shows another example:

!LENGTH in module VECTRLEN calculates the length of a 2-D vector.

!The module contains both private and public procedures

MODULE VECTRLEN

PRIVATE SQUARE

PUBLIC LENGTH

CONTAINS

SUBROUTINE LENGTH(x,y,z)

REAL,INTENT(IN) x,y

REAL,INTENT(OUT) z

CALL SQUARE(x,y)

z = SQRT(x + y)

RETURN

END SUBROUTINE

SUBROUTINE SQUARE(x1,y1)

REAL x1,y1

x1 = x1**2

y1 = y1**2

RETURN

END SUBROUTINE

END MODULE

See Also