Private



Attribute Purpose

The private attribute is used to prevent the internal procedures of a module from being accessible to other program units through the use association.



Declaring Variables to be Private

The private attributes main function is to specify what variables and module procedures can not be accessed outside of module. This can be done in one of two ways. First, a private statement could be used to make all of the variables in a module inaccessible to other program units ( except for any variables that are latter declared with the public attribute ) like so:

private

or individual variables could be declared to be private like so:

real, dimension(7), private :: xarray

integer, private :: ldata, num

The Private attribute and Derived Data Types

Just like with the intrinsic FORTRAN data types, the private statement can be used to keep derived data types inaccessible to other program units. But one other interesting feature is that the components of a derived can be made private while the data type itself remains public for use in other program units. If you do this with any derived types, just be aware that you will not be able to do any operations on or with the components of a derived type that has been declared in this manner outside of the module. The only way you will be to do operations involving these data types is with procedures that are available to outside units via the use association. To get a better idea of what I mean, take a look at the program fragment below.

Module global

public

type person

private

character*15 first, last

integer age

end type person

contains

Subroutine initialize (name,f,l,a)

character*15 f,l

integer a

type(person) :: name

name = person(f,l,a)

end subroutine initialize

end module global

c

program test

use global

implicit none

type(person) :: Nicole

call initialize(Nicole,"Nicole","Smith",27)



Notice how the call to the subroutine initialize had to be made in order to assign values to the components of the variable Nicole. This illustrates perfectly what I stated earlier.



For more information and examples see