Reputation: 27219
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;
}
}
Upvotes: 10
Views: 2603
Reputation: 155
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.
Upvotes: 3
Reputation: 692231
When the PropName class is loaded by the class loader. Enum constants are static final fields of their class.
Upvotes: 5
Reputation: 382464
It's created when the class is loaded, just like any static code block.
Upvotes: 11