Posts

Showing posts with the label Enumeration or enum in Java

Enumeration or enum in Java

Enumeration : An enumeration is a list of named constants. Or Enumeration special type which contains Group of named constants. Enumeration can have constructors, methods and instance variables. Syntax: enum Variable_Name  {  VALUE_1, VALUE_2, VALUE_3, …  } An enumeration is created using the keyword enum. Program: classEnumExample { public enum Season { WINTER, SPRING, SUMMER, FALL } public static void main(String[] args) { for (Season s : Season.values()) System.out.println(s); } } Output: WINTER SPRING SUMMER FALL The following characteristics make enum a ‘special’ class: enum constants cannot be overridden  enum doesn’t support the creation of objects enum can’t extend other classes enum can implement interfaces like classes. Explain restrictions on using enu m in java? Each enum declaration declares an enum class with the following restrictions: 1. enum constants are implicitly final, because they declare constants that should...