Accessibility of symbols declared in a module can be controlled using private
and public
attributes and statement.
Syntax of the statement form:
!all symbols declared in the module are private by default
private
!all symbols declared in the module are public by default
public
!symbols in the list will be private
private :: name1, name2
!symbols in the list will be public
public :: name3, name4
Syntax of the attribute form:
integer, parameter, public :: maxn = 1000
real, parameter, private :: local_constant = 42.24
Public symbols can be accessed from program units using the module, but private symbols cannot.
When no specification is used, the default is public
.
The default access specification using
private
or
public
can be changed by specifying different access with entity-declaration-list
public :: name1, name2
or using attributes.
This access control also affects symbols imported from another module:
module mod1
integer :: var1
end module
module mod2
use mod1, only: var1
public
end module
program test
use mod2, only: var1
end program
is possible, but
module mod1
integer :: var1
end module
module mod2
use mod1, only: var1
public
private :: var1
end module
program test
use mod2, only: var1
end program
is not possible because var
is private in mod2
.