Variable Scopes

MJ variables have a scope that matches MAPPER's global, environmental and RUN variables:

The scope of a variable is passed to the constructor of the variable class and cannot be changed. For example:

// LDV <*global>i6=1001 .
MJNamedInteger globalVar = new MJNamedInteger("_global", VariableScope.GLOBAL,
  6, Long.valueOf(1001));
assert globalVar.scope() == VariableScope.GLOBAL;

// LDV <$environment>i6=2002 .
MJNamedInteger envVar = new MJNamedInteger("$environment", VariableScope.ENVIRONMENT,
  6, Long.valueOf(2002));
assert envVar.scope() == VariableScope.ENVIRONMENT;

// @LDV V22i5=1234 .
MJNumberedInteger int22 = new MJNumberedInteger(22, VariableScope.LOCAL,
  5, EnumSet.noneOf(LoadOption.class), "1234");
assert int22.scope() == VariableScope.LOCAL;

Variable scopes are directly related to MJ variable namespaces; both variables and namespaces have a scope, and the variables registered with a given namespace must have the same scope as the namespace.

In a Java context, MJ variables with VariableScope.GLOBAL and VariableScope.ENVIRONMENT scopes are shared among the classes in an application. A Java class that is analagous to a MAPPER RUN hosts locally scoped MJ variables, or VariableScope.LOCAL.