Step 1: create an enum class Departments as shown below.
package com.javaforjob.Enum;
public enum Departments
{
//Declaration of enum values.These comma separated values should match with data types in the constructor.
CSE("cse","computer science and engineering", 1 ),
IT("Information technology","it",2),
ECE("ece","electronics and communication engineering", 3),
EEE("eee","electrical and electronics engineering", 4);
//declaration of variables
private String name = null;
private int code = -1;
private String desc = null;
//Declaration of constructor
private Departments(String name,String desc,int code)
{
this.name = name;
this.code = code;
this.desc = desc;
}
//Declaration of getters
public String getName()
{
return name;
}
public int getCode()
{
return code;
}
public String getDesc()
{
return desc;
}
}
package com.javaforjob.Enum;
public enum Departments
{
//Declaration of enum values.These comma separated values should match with data types in the constructor.
CSE("cse","computer science and engineering", 1 ),
IT("Information technology","it",2),
ECE("ece","electronics and communication engineering", 3),
EEE("eee","electrical and electronics engineering", 4);
//declaration of variables
private String name = null;
private int code = -1;
private String desc = null;
//Declaration of constructor
private Departments(String name,String desc,int code)
{
this.name = name;
this.code = code;
this.desc = desc;
}
//Declaration of getters
public String getName()
{
return name;
}
public int getCode()
{
return code;
}
public String getDesc()
{
return desc;
}
}
Step 2: create a test class to invoke the above enum class as shown below
package com.javaforjob.Enum;
public class TestEnum {
public static void main(String[] args) {
//accessing the enum values by using the getters
System.out.println(Departments.CSE.getCode() +" "+Departments.CSE.getName()+" "+Departments.CSE.getDesc());
System.out.println(Departments.CSE);
}
}
No comments :
Post a Comment