ENUMS
Enums are present inside the lang package. We no need to import explicitly. It will get imported automatically. It is nothing but group of predefined constants. It is something like our own data type. It can't be changed. By default they are public static and final. After compilation of the code, Enums will also generate the .class files. It's a way of creating our own data types. usually the values inside the Enums will be denoted in uppercase letters to denote that those are constants. Enums can be created inside the class as well as outside of the class. and when Enums are created, It will extent parent ENUM class automatically and ordinal() and Values methods will be inherited as well.
Example:
Enum week {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY; }
Inside Enums we can have Constructors, methods, fields or instance variables, constants
=> when we call week.MONDAY, It will create an object and constructor will be called automatically. and the constructor call will happen 7 times. It means the number of times constructor calling depends on the constants declared in the Enum.
The constants inside the Enum will be be acting as final reference variable and we cannot create an object of Enums explicitly
From java 1.5, we can use Enums in switch statements.
Comments
Post a Comment