Details
-
Bug
-
Status: Open
-
Minor
-
Resolution: Unresolved
-
3.0.8, 4.0.4
-
None
-
None
-
None
-
openjdk 11.0.12 2021-07-20
Description
Hi, I've encountered a small issue - assignments to static fields and executions of static blocks do not happen in the same order they do in Java. I'm not sure if they should, hence the ticket. In Java the assignments and executions are in the declaration order, while Groovy seems to initialize fields first. Attached sample java class, executable in groovy too.
import java.util.*; class ListSupplier{ public static List getList(){ System.out.println("Getting list"); return new ArrayList<>(); } } class ListThenStatic{ static final List list = ListSupplier.getList(); static{ System.out.println("Static block"); } } class StaticThenList{ static{ System.out.println("Static block"); } static final List list = ListSupplier.getList(); } class Test { public static void main(String[] args) { ListThenStatic listThenStatic = new ListThenStatic(); StaticThenList staticThenList = new StaticThenList(); } }
Java produces output:
- Getting list
- Static block
- Static block
- Getting list
From Groovy:
- Getting list
- Static block
- Getting list
- Static block
Is this behaviour expected and/or documented anywhere?
Attachments
Issue Links
- is related to
-
GROOVY-6498 Static initializer not called before static property resolution
- Open
-
GROOVY-7025 Static member in enum class definition not initialized before enums created
- Closed