[records] Compile-time constants can be used in local classes static members but cannot be used in records

Brian Goetz brian.goetz at oracle.com
Wed Jan 29 14:44:14 UTC 2020


Tagir is right, and this is yet another aspect of our technical debt 
with regard to nesting.  Nested entities of all stripes (static vs 
instance, class vs interface vs record vs enum) can access static 
members from lexically enclosing contexts.



On 1/29/2020 2:03 AM, Tagir Valeev wrote:
> But:
>
> 1. Compile time constant is not the state.
> 2. Static field of local class also cannot capture any state, even
> technically (because there's no place to store the state). Yet, this code
> is allowed. I observe asymmetry here.
>
> With best regards,
> Tagir Valeev.
>
> ср, 29 янв. 2020 г., 10:39 Vicente Romero <vicente.romero at oracle.com>:
>
>> Hi Tagir,
>>
>> This behavior is in accordance with the spec: a local record is not a
>> local class, local records are not allowed to capture any state from its
>> enclosing type. We plan to revisit this in the future not only for
>> record but as a general effort on nested-ness but for the time being we
>> preferred to let local records immune to any capture rule that applies
>> to local classes
>>
>> Vicente
>>
>> On 1/28/20 10:01 PM, Tagir Valeev wrote:
>>> Hello!
>>>
>>> This code is perfectly compilable (javac 14-ea+33-1439):
>>>
>>> public class RecordTest {
>>>       public static void main(String[] args) {
>>>           final int x = 2;
>>>           class X {
>>>               static final int y = x;
>>>           }
>>>       }
>>> }
>>>
>>> This is not (simply changed class to record, leaving everything else the
>> same):
>>> public class RecordTest {
>>>       public static void main(String[] args) {
>>>           final int x = 2;
>>>           record X() {
>>>               static final int y = x;
>>>           }
>>>       }
>>> }
>>>
>>> Error:(5, 34) java: non-static variable x cannot be referenced from a
>>> static context
>>>
>>> This looks counter-intuitive to me: why static field in one case can
>>> refer to compile-time constant and in another case cannot?
>>>
>>> With best regards,
>>> Tagir Valeev
>>



More information about the amber-dev mailing list