The PRIVATE and PUBLIC attributes specify the accessibility of entities in a module. (These attributes are also called accessibility attributes.)
The PRIVATE and PUBLIC attributes can be specified in a type declaration statement or a PRIVATE or PUBLIC statement, and take one of the following forms:
Type Declaration Statement:
Statement:
In statement form, an entity can also be a generic identifier (a generic name, defined operator, or defined assignment).
The PRIVATE and PUBLIC attributes can only appear in the scoping unit of a module.
Only one PRIVATE or 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 PUBLIC or PRIVATE statements are specified in a module, the default is PUBLIC accessibility. Entities with PUBLIC accessibility can be accessed from outside the module by means of a USE statement.
If a derived type is declared PRIVATE in a module, its components are also PRIVATE. The derived type and its components are accessible to any subprograms within the defining module through host association, but they are not accessible from outside the module.
If the 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.
Examples
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 derived-type declaration statement indicates that the type is restricted to the module:
TYPE, PRIVATE :: DATA
...
END TYPE DATA
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.
For More Information: