In Java, when is the constructor of an enumeration constant called?
•
Java
To use a creative example in Java, here is the code:
enum Commands{ Save("S"); File("F"); private String shortCut; private Commands(String shortCut){ this.shortCut = shortCut; } public String getShortCut(){ return shortCut; } }
I have the following test / driver code:
public static void main(String args[]){ System.out.println(Commands.Save.getShortCut()); }
The question is: in Java, when do I call the constructor of enumeration constants? In the above example, I only use save enumeration constants Does this mean that the constructor can only be called once to create a save? Or build save and file together?
Solution
The constructor is called when the enumeration class is initialized Each constructor will be called in member declaration order, regardless of which members are actually referenced and used
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码