16.7. Instance Scope Expressions

An instance scope expression evaluates a subexpression in the context of an instance scope. Their use is comparable to the with-statement of the Basic and Pascal programming languages.

Instance Scope Expressions
[49]InstanceScopeExpression::=SelectorExpression '.' '(' Expression ')'  

The compile-time type of the expression e to the left of the period has to be a reference type, or a compile-time error occurs. The evaluation proceeds in several steps:

  1. The expression e is evaluated. If the result is null, a NullPointerException is thrown.

  2. The result is assigned to a final local variable i which is implicitly declared by the instance scope expression and has the compile-time type of e. The scope of i is the parenthesised expression. Its name is the special identifier $ if no local variable of that name is in scope, or an internal name which cannot conflict with legal identifiers.

  3. The right expression is evaluated, its result is discarded. Within this expression, all field and method members of the compile-time type of i are in scope.

  4. Type and result of the instance scope expression are those of i.

The following are examples for instance scope expressions:

container.add(new JCheckBox("foo").(setHorizontalAlignment(bar), setFont(myFont)));

System.out.(println(line1), println(line2), println(line3));