[Solved] error in the enum class [closed]


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 use status in this context (you are using + on String) or status.toString() in other contexts (where String types are expected).

Other (unrelated) problems

  • fields/variables/parameter in java normally start lowercase (id, firstName etc)
  • fields are typically private and not protected
  • most people prefer spaces around the operators such as assignments (a = b)
  • (IMHO) use final when you can (like val, 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]