JAVA LECTURE-13: ENUM
Agenda Agenda 1. Introduction 2. Internal implementation of enum 3. Declaration and usage of enum 4. enum vs switch statement 5. Allowed modifiers for outer and inner enum 6. enum vs inheritance
Agenda Agenda 7. values() method 8. ordinal() method 9. Speciality of java enum 10. Enum vs constructor 11. enum Vs Enum Vs Enumeration
Introduction • Enum: An enum is a special "class" that represents a group of constants (unchangeable variables). If we want to represent a group of named constants then, we should go for Enum. Example: enum Month{ Jan, Feb, Mar, ………, Dec; } • The main objective of enum is to define our own datatypes(Enumerated datatypes). • Enum concept introduced in 1.5 version. • When compared with the old languages enum, JAVA enum is more powerful
Internal implementation of Enum • Every enum is internally implemented by using class concept. • Every enum constant represents an object of the type enum. • Enum is a datatype for constant values. • Enums can be declared inside or outside of a class. • Values inside an enum automatically gets assigned by values serially from 0,1,2… etc). • Also we can assign values in enum constants. • Every enum constant is always public static final. • Like a class, enum can hold methods, variables and blocks. • Enum object has three methods, such as values(), ordinal() and valueOf().
Enum declaration and usage • Every enum constant is always public static final and hence we can access enum constant by using enum name. • Example: enum Car{ BMW, TESLA, AUDI, FORD; } class Test { public static void main(String[] args) { Car c = Car.TESLA; System.out.println(c); } } Output: TESLA
Enum declaration and usage • Inside enum, toString() method is internally implemented to return name of the constant. • We can declare enum either within the class or outside of the class but not inside a method. If we are trying to declare inside a method, then we will get compile time error saying, “enum types must not be local”
Possible enum types (Outer and Inner enum) 1. enum x { …… } class y { …………….. …………….. …………….. } Correct syntax 2. class y { enum x { …… } } Correct syntax 3. class y { public void m1() { enum x { …… } } } Compile time error: “enum types must not be local”
Allowed modifiers for Outer and Inner enum • If we declare enum outside of the class, the applicable modifiers are: public, default and strictfp. • If we declare enum inside a class, the applicable modifiers are: public, default, strictfp, private, protected and static. public <default> stricfp public <default> stricfp private protected static
enum vs switch • Until 1.4v, the allowed argument types for the switch statement are byte, short, char and int. But from 1.5v onwards, corresponding wrapper classes and enum types are also allowed. From 1.7v onwards, string type also allowed. • Hence, from 1.5v onwards, we can pass enum type as argument to switch statement. 1.4v 1.5v 1.7v byte Byte short Short char Character String int Integer enum
enum vs switch • If we pass enum type as argument to switch statement, then every case label should be valid enum constant. Otherwise we will get compile time error. • Example: switch(c) { case BMW: case TESLA: case AUDI: case FORD: case NAYEEM: (Error: unqualified enumeration; constant name required }
enum vs inheritance • Every enum is always direct child class of java.lang.Enum and hence our enum can’t extend any other enum.(Because java won’t provide multiple inheritance) • Every enum is always final implicitly and hence for our enum, we can’t create child enum. • Because of above reasons, we can conclude that, inheritance concept not applicable for enum explicitly and we can not use extends keyword for enum. Otherwise we will get compile time error saying, “enum types are not extensible” or “cannot inherit from final enumClassNAme”
enum vs inheritance • Anyway, an enum can implement any number of interfaces interface x{ } enum e implements x{ } Correct Syntax • Every enum in java is the direct child class of java.lang.Enum and hence this class acts as base class for all java enum. • It is an abstract class and it is the direct child class of object. It implements serializable and comparable interfaces.
values() method inside enum class • Every enum implicitly contains values method to list out all values present in enum Car[] b = Car.values(); • Note: Values method not present in java.lang.Enum and object classes. enum keyword implicitly provides this method.
ordinal() method inside enum class • Inside enum, order of contents is important and we can represent this order by using ordinal values. We can find the ordinal value of enum constant by using ordinal() method, public final int ordinal(); • Ordinal value is zero based like array index.
Specialty of JAVA enum • In old languages enum, we can take only constants, but in java enum, in addition to constants we can take methods, constructors, normal variables etc. Hence, java enum is more powerful than old languages enum. • Even inside java enum, we can declare main method and we can run enum class directly from command prompt. Example is given below: OUTPUT: Enum main method
Specialty of JAVA enum • Note: In addition to constants, if we are taking any extra members like a method, then list of constants should be in the first line and should end with a semicolon. Example: Enum Fish{ STAR,GUPPY; Public void m1() { } } Correct Syntax Enum Fish{ STAR,GUPPY Public void m1() { } } Incorrect Syntax: Missing semicolon after the constants. Enum Fish{ Public void m1() { } STAR,GUPPY } Incorrect Syntax: Constants should be declared at the beginning of the enum class
Specialty of JAVA enum • Inside enum, if we are taking any extra member like a method, compulsory first line should contain list of constants or at least semicolon. Enum Fish{ public void m1() { } } Incorrect Syntax: Missing constants or semicolon. Enum Fish{ ; public void m1() { } } Correct Syntax: Constants are missing but at least semicolons are there • Anyway, an empty enum is a valid java syntax
enum vs constructors • An enum can contain constructors. • Enum constructors will be executed separately for every enum constant at the time of enum class loading automatically. • We cannot create enum object directly and hence we can’t invoke enum constructors directly.
enum vs Enum vs Enumeration • enum: enum is a keyword in java which can be used to define a group of named constants. • Enum: Enum is a class in java present in java.lang package. Every enum in java should be direct child class of Enum class. Hence, this class acts as base class of all java enums. • Enumeration: Enumeration is an interface present in java.util package. We can use Enumeration object to get objects one by one from collection.
Conclusion & Questions 1. What are the fixed modifiers for constants inside java enum? 2. Can we extend enum? If not, why? 3. Can Enum extend a class in Java? 4. Can Enum implement an interface in Java? 5. Can we use Enum in the switch case in Java? 6. Can we declare Constructor inside Enum in Java? 7. What does values() method do in Enum? 8. What does ordinal() method do in Enum? 9. What does valueOf() method do in Enum? 10. How to iterate over all instances of an Enum?
Some practice code Output:
Some practice code Output:

Java 8 Syntax : Java Enum lessons for developers

  • 1.
  • 2.
    Agenda Agenda 1. Introduction 2. Internalimplementation of enum 3. Declaration and usage of enum 4. enum vs switch statement 5. Allowed modifiers for outer and inner enum 6. enum vs inheritance
  • 3.
    Agenda Agenda 7. values() method 8.ordinal() method 9. Speciality of java enum 10. Enum vs constructor 11. enum Vs Enum Vs Enumeration
  • 4.
    Introduction • Enum: Anenum is a special "class" that represents a group of constants (unchangeable variables). If we want to represent a group of named constants then, we should go for Enum. Example: enum Month{ Jan, Feb, Mar, ………, Dec; } • The main objective of enum is to define our own datatypes(Enumerated datatypes). • Enum concept introduced in 1.5 version. • When compared with the old languages enum, JAVA enum is more powerful
  • 5.
    Internal implementation ofEnum • Every enum is internally implemented by using class concept. • Every enum constant represents an object of the type enum. • Enum is a datatype for constant values. • Enums can be declared inside or outside of a class. • Values inside an enum automatically gets assigned by values serially from 0,1,2… etc). • Also we can assign values in enum constants. • Every enum constant is always public static final. • Like a class, enum can hold methods, variables and blocks. • Enum object has three methods, such as values(), ordinal() and valueOf().
  • 6.
    Enum declaration andusage • Every enum constant is always public static final and hence we can access enum constant by using enum name. • Example: enum Car{ BMW, TESLA, AUDI, FORD; } class Test { public static void main(String[] args) { Car c = Car.TESLA; System.out.println(c); } } Output: TESLA
  • 7.
    Enum declaration andusage • Inside enum, toString() method is internally implemented to return name of the constant. • We can declare enum either within the class or outside of the class but not inside a method. If we are trying to declare inside a method, then we will get compile time error saying, “enum types must not be local”
  • 8.
    Possible enum types(Outer and Inner enum) 1. enum x { …… } class y { …………….. …………….. …………….. } Correct syntax 2. class y { enum x { …… } } Correct syntax 3. class y { public void m1() { enum x { …… } } } Compile time error: “enum types must not be local”
  • 9.
    Allowed modifiers forOuter and Inner enum • If we declare enum outside of the class, the applicable modifiers are: public, default and strictfp. • If we declare enum inside a class, the applicable modifiers are: public, default, strictfp, private, protected and static. public <default> stricfp public <default> stricfp private protected static
  • 10.
    enum vs switch •Until 1.4v, the allowed argument types for the switch statement are byte, short, char and int. But from 1.5v onwards, corresponding wrapper classes and enum types are also allowed. From 1.7v onwards, string type also allowed. • Hence, from 1.5v onwards, we can pass enum type as argument to switch statement. 1.4v 1.5v 1.7v byte Byte short Short char Character String int Integer enum
  • 11.
    enum vs switch •If we pass enum type as argument to switch statement, then every case label should be valid enum constant. Otherwise we will get compile time error. • Example: switch(c) { case BMW: case TESLA: case AUDI: case FORD: case NAYEEM: (Error: unqualified enumeration; constant name required }
  • 12.
    enum vs inheritance •Every enum is always direct child class of java.lang.Enum and hence our enum can’t extend any other enum.(Because java won’t provide multiple inheritance) • Every enum is always final implicitly and hence for our enum, we can’t create child enum. • Because of above reasons, we can conclude that, inheritance concept not applicable for enum explicitly and we can not use extends keyword for enum. Otherwise we will get compile time error saying, “enum types are not extensible” or “cannot inherit from final enumClassNAme”
  • 13.
    enum vs inheritance •Anyway, an enum can implement any number of interfaces interface x{ } enum e implements x{ } Correct Syntax • Every enum in java is the direct child class of java.lang.Enum and hence this class acts as base class for all java enum. • It is an abstract class and it is the direct child class of object. It implements serializable and comparable interfaces.
  • 14.
    values() method insideenum class • Every enum implicitly contains values method to list out all values present in enum Car[] b = Car.values(); • Note: Values method not present in java.lang.Enum and object classes. enum keyword implicitly provides this method.
  • 15.
    ordinal() method insideenum class • Inside enum, order of contents is important and we can represent this order by using ordinal values. We can find the ordinal value of enum constant by using ordinal() method, public final int ordinal(); • Ordinal value is zero based like array index.
  • 16.
    Specialty of JAVAenum • In old languages enum, we can take only constants, but in java enum, in addition to constants we can take methods, constructors, normal variables etc. Hence, java enum is more powerful than old languages enum. • Even inside java enum, we can declare main method and we can run enum class directly from command prompt. Example is given below: OUTPUT: Enum main method
  • 17.
    Specialty of JAVAenum • Note: In addition to constants, if we are taking any extra members like a method, then list of constants should be in the first line and should end with a semicolon. Example: Enum Fish{ STAR,GUPPY; Public void m1() { } } Correct Syntax Enum Fish{ STAR,GUPPY Public void m1() { } } Incorrect Syntax: Missing semicolon after the constants. Enum Fish{ Public void m1() { } STAR,GUPPY } Incorrect Syntax: Constants should be declared at the beginning of the enum class
  • 18.
    Specialty of JAVAenum • Inside enum, if we are taking any extra member like a method, compulsory first line should contain list of constants or at least semicolon. Enum Fish{ public void m1() { } } Incorrect Syntax: Missing constants or semicolon. Enum Fish{ ; public void m1() { } } Correct Syntax: Constants are missing but at least semicolons are there • Anyway, an empty enum is a valid java syntax
  • 19.
    enum vs constructors •An enum can contain constructors. • Enum constructors will be executed separately for every enum constant at the time of enum class loading automatically. • We cannot create enum object directly and hence we can’t invoke enum constructors directly.
  • 20.
    enum vs Enumvs Enumeration • enum: enum is a keyword in java which can be used to define a group of named constants. • Enum: Enum is a class in java present in java.lang package. Every enum in java should be direct child class of Enum class. Hence, this class acts as base class of all java enums. • Enumeration: Enumeration is an interface present in java.util package. We can use Enumeration object to get objects one by one from collection.
  • 21.
    Conclusion & Questions 1.What are the fixed modifiers for constants inside java enum? 2. Can we extend enum? If not, why? 3. Can Enum extend a class in Java? 4. Can Enum implement an interface in Java? 5. Can we use Enum in the switch case in Java? 6. Can we declare Constructor inside Enum in Java? 7. What does values() method do in Enum? 8. What does ordinal() method do in Enum? 9. What does valueOf() method do in Enum? 10. How to iterate over all instances of an Enum?
  • 22.
  • 23.