Static fields in specialized classes

Paul Sandoz paul.sandoz at oracle.com
Thu Oct 16 11:57:45 UTC 2014


On Oct 16, 2014, at 11:58 AM, Remi Forax <forax at univ-mlv.fr> wrote:

> 
> On 10/15/2014 04:37 PM, Brian Goetz wrote:
>> Actually we're all being silly.  Referencing the field at all will trigger initialization.  No extra kicking needed.
>> 
>> So if we have:
>> 
>> class Foo<any T> {
>>    static int x = 3;
>> 
>>    void foo() { ... x ... }
>> }
>> 
>> the reference to Foo.x in Foo<int> will trigger initialization of Foo, causing its static initializers to run.
> 
> yes, but not at the right time :(
> 

That depends :-) From what i gather we don't have to bake in an intialization dependency between the template class and the specialized class, so perhaps we should not set any expectations, especially if such expectations falsely suggest a hierarchical class relationship when there is none.

Paul.

> class Foo<any T> {
>    static int x;
>    static {
>       System.out.println("init");
>       x = 2;
>    }
> 
>    void foo() {
>      System.out.println("foo");
>      ... x ...
>    }
> }
> 
> new Foo<int>().foo()
> 
> will print "foo" then "init" instead of "init" then "foo".
> 
> RĂ©mi



More information about the valhalla-dev mailing list