java - Array as Enum member fails compilation -


this doesn't work:

    public enum testenum {      foo({"foobar","foobaz"}); //this fails      private final string[] indicators;      testenum(string[] indicators){      this.indicators = indicators;     }      private string[] getindicators(){     return this.indicators;     } 

the code above fails compile. 1 below compiles successfully

    public enum testenum {  foo(new string[3]); //this okay. why?  private final string[] indicators;  testenum(string[] indicators){     this.indicators = indicators; }  private string[] getindicators(){     return this.indicators; } 

my objective here provide enum can represented multiple strings. aware of enummap, won't work here mapping i'm interested in <string,enum>. i'm aware use plain map. guess i'm curious why no. 1 won't work me.

the expression {"foobar","foobaz"} array initializer , work when directly declaring array.

try prepending new string[]:

foo(new string[] {"foobar","foobaz"}); 

according jls, section 10.6:

an array initializer may specified in declaration (§8.3, §9.3, §14.4), or part of array creation expression (§15.10), create array , provide initial values.

that is, need array creation expression, section 15.10.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -