On Friday, August 23, 2019 11:25:05 PM CEST Gavin Bierman wrote:
> A draft language spec for records is available at:
> 
> http://cr.openjdk.java.net/~gbierman/8222777/8222777-20190823/specs/records-> 
> jls.html

Hi Gavin,

When reading the spec, the following caught my attention:

"""A nested record type is implicitly static. It is permitted for the 
declaration of a nested record type to redundantly specify the static 
modifier.

    This implies that it is impossible to declare a record type in the body of 
an inner class (8.1.3), because an inner class cannot have static members 
except for constant variables.
"""

So in this respect, record type is similar to enum type which is also 
implicitly static.

Although the JLS does not mention explicitly (or at least I can't find it), 
enum types are not allowed as local types (declared inside methods, 
constructors or initializers). For example:

public class Test {
    static void method() {
        enum E {A, B} // Error: enum types must not be local
    }
}

Are record types the same? I never quite understood why:
- it is not possible to declare static local classes (enum types included)
- declare static members in inner classes

Sometimes you would just want a simple static data holder with local scope 
(for example an intermediate data element in some Stream<T> stage). And 
records would be perfect for that with their concise syntax.

But if there is some deeper reason why static members are not allowed in 
"local" context(s) then perhaps record types should behave the same as enum 
types.


Regards, Peter



Reply via email to