java - How to use constructor in enum? -
i want use enum create singleton class in java. should this:
public enum mysingleton implements myinterface { instance; private final myobject mystring; private mysingleton(myobject mystring) { this.mystring= mystring; } }
looks cannot use parameters in constructor. there workaround this? in advance
yor enum wrong. below correct declaration:
public class hello { public enum myenum { one("one value"), two("two value"); //here elements of enum. private string value; private myenum(string value) { this.value = value; system.out.println(this.value); } public string getvalue() { return value; } } public static void main(string[] args) { myenum e = myenum.one; } }
output:
one value 2 value
conctructor invoked each element of enum.
Comments
Post a Comment