Scope modifiers: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Java example, more explanation)
(Ada added)
Line 3: Line 3:
Show the different scope modifiers available in your language and briefly explain how they change the scope of their variable or function. If your language has no scope modifiers, note it.
Show the different scope modifiers available in your language and briefly explain how they change the scope of their variable or function. If your language has no scope modifiers, note it.


=={{header|Ada}}==
===Public and private declarative parts===
In [[Ada]] declarative region of a package has publicly visible and private parts. The private part is introduced by '''private''':
<lang ada>
package P is
... -- Declarations placed here are publicly visible
private
... -- These declarations are visible only to the children of P
end P;
</lang>
Correspondingly a type or object declaration may be incomplete in the public part providing an official interface. For example:
<lang ada>
package P is
type T is private; -- No components visible
procedure F (X : in out T); -- The only visible operation
N : constant T; -- A constant, which value is hidden
private
type T is record -- The implementation, visible to children only
Component : Integer;
end record;
procedure V (X : in out T); -- Operation used only by children
N : constant T := (Component => 0); -- Constant implementation
end P;
</lang>
===Bodies (invisible declarations)===
The keyword '''body''' applied to the packages, protected objects and tasks. It specifies an implementation of the corresponding entity invisible from anywhere else:
<lang ada>
package body P is
-- The implementation of P, invisible to anybody
procedure W (X : in out T); -- Operation used only internally
end P;
</lang>
===Private children===
The keyword '''private''' can be applied to the whole package, a child of another package:
<lang ada>
private package P.Q is
... -- Visible to the siblings only
private
... -- Visible to the children only
end P.Q;
</lang>
This package can be then used only by private siblings of the same parent P.
=={{header|Java}}==
=={{header|Java}}==
<lang java>public //any class may access this member directly
<lang java>public //any class may access this member directly

Revision as of 18:51, 10 June 2009

Task
Scope modifiers
You are encouraged to solve this task according to the task description, using any language you may know.

Most programming languages offer support for subroutines. When execution changes between subroutines, different sets of variables and functions ("scopes") are available to the program. Frequently these sets are defined by the placement of the variable and function declarations ("lexical scoping"). These sets may also be defined by special modifiers to the variable and function declarations.

Show the different scope modifiers available in your language and briefly explain how they change the scope of their variable or function. If your language has no scope modifiers, note it.

Ada

Public and private declarative parts

In Ada declarative region of a package has publicly visible and private parts. The private part is introduced by private: <lang ada> package P is

  ... -- Declarations placed here are publicly visible

private

  ... -- These declarations are visible only to the children of P

end P; </lang> Correspondingly a type or object declaration may be incomplete in the public part providing an official interface. For example: <lang ada> package P is

  type T is private; -- No components visible
  procedure F (X : in out T); -- The only visible operation
  N : constant T; -- A constant, which value is hidden

private

  type T is record -- The implementation, visible to children only
     Component : Integer;
  end record;
  procedure V (X : in out T); -- Operation used only by children
  N : constant T := (Component => 0); -- Constant implementation

end P; </lang>

Bodies (invisible declarations)

The keyword body applied to the packages, protected objects and tasks. It specifies an implementation of the corresponding entity invisible from anywhere else: <lang ada> package body P is

  -- The implementation of P, invisible to anybody
  procedure W (X : in out T); -- Operation used only internally

end P; </lang>

Private children

The keyword private can be applied to the whole package, a child of another package: <lang ada> private package P.Q is

  ... -- Visible to the siblings only

private

  ... -- Visible to the children only

end P.Q; </lang> This package can be then used only by private siblings of the same parent P.

Java

<lang java>public //any class may access this member directly

protected //only this class, subclasses of this class, //and classes in the same package may access this member directly

private //only this class may access this member directly

static //for use with other modifiers //limits this member to one reference for the entire JVM

//adding no modifier allows access to the member by classes in the same package

//method parameters are available inside the entire method

//Other declarations follow lexical scoping, //being in the scope of the innermost set of braces ({}) to it. //You may also create local scopes by surrounding blocks of code with braces.

public void function(int x){

  //can use x here
  int y;
  //can use x and y here
  {
     int z;
     //can use x, y, and z here
  }
  //can use x and y here, but NOT z

}</lang>