10

I have a simple question regarding Enums in Java Please refer to the following code . When do the instances like PropName .CONTENTS get instantiated ?

public enum PropName {

  CONTENTS("contents"),
  USE_QUOTES("useQuotes"),
  ONKEYDOWN("onkeydown"),
  BROWSER_ENTIRE_TABLE("browseEntireTable"),
  COLUMN_HEADINGS("columnHeadings"),
  PAGE_SIZE("pageSize"),
  POPUP_TITLE("popupTitle"),
  FILTER_COL("filterCol"),
  SQL_SELECT("sqlSelect"),
  ;

  private String name;

  private PropName(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }
}
Geek
  • 26,489
  • 43
  • 149
  • 227

3 Answers3

11

It's created when the class is loaded, just like any static code block.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
5

When the PropName class is loaded by the class loader. Enum constants are static final fields of their class.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
3

The enum types' instances get created in the class loader subsystem in the last phase of "loading the class file", called Initialization and not at runtime, like other class instances. They come first, before any other static field/variable initializations and that is why you can't access static fields inside an enum's constructor either, unless they are a constant.

Toni Nagy
  • 133
  • 2
  • 11