Enum in Java





  • A Java Enum is a special Java type used to define collections of constants.

  • More precisely, a Java enum type is a special kind of Java class.

  • An enum can contain constants, methods etc. Java enums were added in Java 5.

  • In the Java programming language, you define an enum type by using the enum keyword. For example, you would specify a days-of-the-week enum type as:

Example :

public enum Day {
  SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
  THURSDAY, FRIDAY, SATURDAY 
}          

Note : All enums implicitly extend java.lang.Enum.

You can refer to the constants in the enum above like this:
Day day = Day.MONDAY;


Specific values to the enum constants :

  • The enum constants have initial value that starts from 0, 1, 2, 3 and so on.

  • But you can initialize the specific value to the enum constants by defining fields and constructors.

Example :

class EnumDemo
{  
 enum Level
 {
  HIGH(5), MEDIUM(10), LOW(15);   
  
  private int value;  
  private Level(int value){  
   this.value=value;  
 }  
}  
public static void main(String []ar)
{ 
 for (Level l : Level.values())  
 System.out.println(l+" "+l.value);  
 }
}          
OUTPUT :
HIGH 5
MEDIUM 10
LOW 15     

Next topic is command-line-input




 




Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in



CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved