I am trying to figure out why I get a compile error on the code below:
package com....;
public enum Something { //says error: <identifier> expected on this line
private String myInput;
public Something(String paramString) {
this.myInput = paramString;
}
public String getInputName() {
return this.myInput;
}
}
asked May 2, 2015 at 15:50
michaelsmith
1,0511 gold badge17 silver badges37 bronze badges
-
did you mean class instead of enum?Nidhoegger– Nidhoegger2015年05月02日 15:51:50 +00:00Commented May 2, 2015 at 15:51
-
It is code I inherited and it comes with enummichaelsmith– michaelsmith2015年05月02日 15:54:25 +00:00Commented May 2, 2015 at 15:54
2 Answers 2
You have couple of problem with your enum declaration , first enum constructor cannot be public and second you need to add ; before private field. E.g.
public enum Something {
;
private String myInput;
Something(String paramString) {
this.myInput = paramString;
}
public String getInputName() {
return this.myInput;
}
}
answered May 2, 2015 at 15:55
sol4me
15.8k6 gold badges37 silver badges35 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
michaelsmith
Aha. Adding ; fixes the problem. Didnt know that there must be a ; before private variable. Learned something. Thank you.
sol4me
Glad it helps :) . You can always accept it as answer if it answered your question
D. Ben Knoble
why is the
; necessary?sol4me
@BenKnoble To end the
enum constant declaration block . Please note that in enum All the constants must be declared on the top, they can't come last or in between enum definitionSince it seems you are not using enumerations, change "enum" to "class".
package com....;
public class Something {
private String myInput;
public Something(String paramString) {
this.myInput = paramString;
}
public String getInputName() {
return this.myInput;
}
}
answered May 2, 2015 at 15:51
Zigac
1,6111 gold badge17 silver badges27 bronze badges
1 Comment
michaelsmith
Changing to class is indeed compiling, I suppose this fixes my problem. But still, why wouldnt it compile with enum?
lang-java