Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7025

Static member in enum class definition not initialized before enums created

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.3.0
    • 3.0.14, 2.5.20, 4.0.7
    • Compiler

    Description

      I have a static member being initialized inside an enum:

      class Foo {
        ...
        enum Bar {
          FIRST("foo")
          ...
          static final Set<String> barNames = new HashSet<String>()
          ...
          Bar(String name) {
            barNames.add(name)
            ...
          }
          ...
        }
        ...
      }
      

      However, when the code above executes, barNames is null inside the constructor, i.e., the static member of the enum class is not initialized before the enums themselves are initialized. This may not be a bug, but it seems to me there should be some documentation and/or a compiler warning if you do this.

      Instead, I had to move the set outside the enum to the enclosing class.

      Attachments

        1. enum_in_class.png
          11 kB
          Paul King

        Issue Links

          Activity

            People

              emilles Eric Milles
              mitsu Mitsu Hadeishi
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: