3

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
2
  • did you mean class instead of enum? Commented May 2, 2015 at 15:51
  • It is code I inherited and it comes with enum Commented May 2, 2015 at 15:54

2 Answers 2

5

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
Sign up to request clarification or add additional context in comments.

4 Comments

Aha. Adding ; fixes the problem. Didnt know that there must be a ; before private variable. Learned something. Thank you.
Glad it helps :) . You can always accept it as answer if it answered your question
why is the ; necessary?
@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 definition
1

Since 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

1 Comment

Changing to class is indeed compiling, I suppose this fixes my problem. But still, why wouldnt it compile with enum?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.