1

I have this code

 enum check { STUDENT ("Sireee"), ID (12354), YEAR ("1st Year"), DEP("College of Computer Studies"); private String year; private String student; private String dep; private int id; // some Constructor here 

my problem is I dont know how to get the values of of STUDENT, ID, YEAR, and DEP in the main class. how to be able to get this values? enter image description here

and the result is enter image description here

my **EXPECTED OUTPUT ** is

Sireee

12345

1st Year

College of Computer Studies

2
  • 3
    This does not look like an appropriate use case for an enum -- it looks like an appropriate use case for a class. Commented Dec 28, 2019 at 3:23
  • 2
    You are using enum the wrong way. It is not used to maintain the state like a class. Define a class named Student that holds name, id, year and dep Commented Dec 28, 2019 at 3:46

2 Answers 2

5

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

1 Comment

Thank you very much sir <3 tho I also tried different ways and it worked thank you for you insights
0

enter image description here

I was able to solved this problem thanks to all your answers and I did tried some experiments and I was amazed that it really works <3 enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.