How to Use Enum when you program in Java
Many dont use enums in the programming methodology they follow.Enums can be poweerful than they
are thought of. We can use enum to create a set of valid values for a field or a method. For example, in
a typical application ,
the only possible values for the
Student_type are single_degree or dual_degree.
Suppose that the single degree means a B.Tech Degree and a Dual degree means a
Btech+Mtech degree.
Hence the state value will hold only two values one for
the single degree students and the other for the dual degree students. It can be referenced from multiple
places in our application.
Here is a short example of enum
package Enumdemo;
public enum StudentType
{
SINGLE_DEGREE,
DUAL_DEGREE
}
Just to remind that Enum values are case sensitive and as per the conventions they should be capitalized.
Values inside the enum should be separated by comma and values can be written on a single or can be
in multiple lines.As per the above declaration enum initializes its values by ’1′ if iot not initialized while
giving definition.
So the SINGLE_DEGREE holds value 1 and DUAL_DEGREE holds value 2.We didn’t even had to declare those
but this is the feature enum posseses and proves worthy many times.
Now we define the class which gonna use the enum types mentioned above.
package Enumdemo;
public class StudentsOfTheYear
{
public String StudentName;
public StudentType studenttype;
public String address;
public StudentOfTheYear()
{
}
public void showStudentDetails(StudentOfTheYear student)
{
System.out.println(“Student Name -”+student.StudentName);
System.out.println(“Student Type -”+student.StudentType);
System.out.println(“Student Address -”+student.StudentAddress);
}
public static void main(String args[])
{
StudentsOfTheYear student;
student.StudentName=”Alia”;
student.StudentType=student.SINGLE_DEGREE;
student.StudentAddress=”158,Churchgate,Mumbai”;
new StudentOfTheYear(student);
student.StudentName=”Aarav”
student.StudentType=student.DUAL_DEGREE;
studentStudentAddress=”471,NCR,Delhi”;
}
}
Here in the main function student type has been assigned by using enum. As per programming methodology , the code should be written in such a manner that other developers too should be able to understand. It increases the ability of understanding when other programmers read your code.
Below the snapshot gives a better view of this.
Hope you are able to read the image. IDE used the snapshot is Netbeans which is not preferred more than eclipse.
But i used that from the very beginning and mine favourite too and it may the favourite of many others too.
https://plus.google.com/111792315181329874552/


















