[sealed-classes] Spec for next version of Sealed Classes

Remi Forax forax at univ-mlv.fr
Fri Oct 23 17:17:08 UTC 2020


----- Mail original -----
> De: "Gavin Bierman" <gavin.bierman at oracle.com>
> À: "amber-spec-experts" <amber-spec-experts at openjdk.java.net>
> Envoyé: Vendredi 23 Octobre 2020 17:16:20
> Objet: [sealed-classes] Spec for next version of Sealed Classes

> Dear all:
> 

Hi Gavin,

> Drafts of the specs for the Sealed Classes feature that we plan to preview for
> a second time in JDK 16 are now available:
> 
> http://cr.openjdk.java.net/~gbierman/8246775/latest/
> 
> [NB: The URL will change once we have a JEP number, and will be announced.]
> 
> The changes are the same as those in the first preview that was released in Java
> SE 15, except for minor editorial changes and the following:
> 
> - Clarification the use of context when applying the lexical grammar,
>  particularly in the identification of contextual keywords (formerly described
>  as "restricted identifiers" and "restricted keywords"). This is detailed in a
>  companion document entitled “Contextual Keywords". The keywords `sealed`,
>  `non-sealed`, and `permits` are now defined as new instances of contextual
>  keywords (3.9).
> 
> - This spec now assumes that the changes detailed in companion documents
>  entitled "Consistent Class and Interface Terminology” and "Local and Nested
>  Static Declarations" have been applied (these are being introduced as part of
>  the Records JEP). In particular, this means that Java SE 16 will support
>  static declarations in two new positions:
> 
>    1. Local, implicitly-static interfaces and enum classes
>    2. Static members of inner classes

So now the lazy class init idiom the can be written like this

public class DB {
  public static DB getInstance() {
    class Holder {
      private static final DB INSTANCE = new DB();
    }
    return Holder.INSTANCE;
  }
}

Question, does the compiler add a hidden field inside Holder that reference DB ?

If yes, then declaring the class Holder static should remove that field like with with a record

public class DB {
  public static DB getInstance() {
    record Holder() {
      private static final DB INSTANCE = new DB();
    }
    return Holder.INSTANCE;
  }
}

>   
>  This requires asserting that local interfaces are not permitted to be
>  `sealed`. (14.3)
>  
> - To enhance narrowing reference conversion to allow for stricter checking of
>  cast conversions with respect to sealed type hierarchies (5.1.6.1).

the discussion is clear, i'm wondering if you are using the term "named" class because anonymous classes are not declared final (as a weird twist of the history).

> 
> - Local classes are not considered when determining implicitly declared
>  permitted direct subclasses of a `sealed` class or `sealed` interface
>  (8.1.6, 9.1.4).
> 
> 
> Comments welcome!
> 
> Gavin

Rémi


More information about the amber-spec-observers mailing list