You are using enum the wrong way. In your case, it is preferable to use a class
Student.java
public class Student { private int id; private String name; private String year; // int is prefered. since you are using string I used it. private Department dep; // demonstrating use of enum // getters and setters }
Now, let's see how we can use enum
Department.java
public enum Department { CSE("Computer Science Engineering"), MEC("Mechanical Engineering") private String name; Department(String name) { this.name = name; } public String getName() { return name; } }
If you wish to display the values now, lets create two students and display the values
public class Test { public static void main(String[] args) { Student s1 = new Student(); s1.setName("John Wick"); s1.setId(1); s1.setYear("1st Year"); s1.setDepartment(Department.CSE); Student s2 = new Student(); s2.setName("Will Smith"); s2.setId(2); s2.setYear("2nd Year"); s2.setDepartment(Department.MEC); System.out.println(s1.getName() + " department is " + s1.getDepartment().getName()); System.out.println(s2.getName() + " department is " + s2.getDepartment().getName()); } }
Output:
John Wick department is Computer Science Engineering Will Smith department is Mechanical Engineering
enumthe wrong way. It is not used to maintain the state like aclass. Define a class namedStudentthat holdsname,id,yearanddep