The Artima Developer Community
Sponsored Link

Pattern Centric Blog
Lexical Scope of Java Inner Classes and Closures
by Howard Lovatt
May 26, 2007
Summary
One of the key differences between the different Java inner class and closure proposals is the lexical scope of names, this blog compares the scoping of the major proposals (CICE + ARM, C3S, FCM + JCA, and BGGA in both its forms)

Advertisement

Lexical Scope of Inner Classes and Closures

One of the key differences between the different Java inner class and closure proposals is the lexical scope of names, this blog compares the scoping of the major proposals (CICE + ARM, C3S, FCM + JCA, and BGGA in both its forms). The order in the comparison table below, from left to right, is how large a change is made to Java. There are two main change groups: CICE and C3S do not change the semantics of inner class methods, whereas FCM and BGGA do. All the proposals add syntactic changes to make the declaration of inner classes shorter. The comparison is similar to that given by Stephen Colebourne in his blog, but includes the inherited scope as well as the enclosing scope. (Stephen Colebourne was going to post the table below, or at least a similar one, on his blog but was travelling at the time.)

A more detailed comparison was given in a previous post, but the table below is a good summary of an important difference (lexical scope).

Property

Inner class

CICE

ARM Extension to CICE

C3S

FCM

JCA Extensions to FCM

Restricted Version of BGGA Closure Conversions

BGGA

Access to names from enclosing scope

Y

(Local variables must be final)

Y

(Local variables must be public to give write access)

Y

Y

Y

Y

Y

(Local variables must be final)

Y

Access to names from inherited classes/interfaces

Y

Y

N

(No inheritance)

Y

N

N

N

N

Inherited names hide enclosing names

Y

Y

N

(No inheritance)

Y

N

N

N

N

Break and continue into the enclosing scope from within the inner class/closure method

N

N

Y

(No inheritance)

Y

(Must be a labelled break or continue to break or continue into the enclosing method)

N

Y

N

Y

Return from the enclosing method (as opposed to returning from the inner class/closure method)

N

N

Y

(No inheritance)

Y

(Must be qualified with method name to return from the enclosing method)

N

Y

N

(Can only return via going of the end of the code block – no return statement allowed)

Y

(Return statement returns from the enclosing method and going of the end of a code block returns from the inner class/closure method)

Talk Back!

Have an opinion? Readers have already posted 23 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Howard Lovatt adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Dr. Howard Lovatt is a senior scientist with CSIRO, an Australian government owned research organization, and is the creator of the Pattern Enforcing Compiler (PEC) for Java. PEC is an extended Java compiler that allows Software Design Patterns to be declared and hence checked by the compiler. PEC forms the basis of Howard's 2nd PhD, his first concerned the design of Switched Reluctance Motors.

This weblog entry is Copyright © 2007 Howard Lovatt. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us