No there is no “error in the enum class”. You have problems in the toString
(see below). You probably need the following
public String toString() {
return "ID: "+ID + " First Name: "+FirstName+" Last Name: " +LastName+" Marital Status: "+ Status +" Age: "+Age;
}
Problems:
- You can’t return
System.out.println(…)
(it returns void, it does not “print-and-return-the-string”) - To get a stringified version for
status
just usestatus
in this context (you are using+
on String) orstatus.toString()
in other contexts (whereString
types are expected).
Other (unrelated) problems
- fields/variables/parameter in java normally start lowercase (
id
,firstName
etc) - fields are typically
private
and notprotected
- most people prefer spaces around the operators such as assignments (
a = b
) - (IMHO) use
final
when you can (likeval
,const
in other languages`) - enum fields are typically in uppercase (
SINGLE
,MARRIED
)
Here is a (more) proper version of your class:
public class Person {
private int id;
private String firstName;
private String lastName;
private MaritalStatus status;
private int age;
public Person(final int id, final String firstName, final String lastName, final MaritalStatus status, final int age) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.status = status;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", firstName="" + firstName + "\'' +
", lastName="" + lastName + "\'' +
", status=" + status +
", age=" + age +
'}';
}
}
p.s. I am guessing this is not production code, as having a field named age
is a bit awkward
solved error in the enum class [closed]