The Artima Developer Community
Sponsored Link

Java Buzz Forum
Enum in java part 2

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Enum in java part 2 Posted: Apr 19, 2016 9:12 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Enum in java part 2
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement

3. Enum Iteration in for-each loop:




  • You can obtain an array of all the possible values of a Java enum type by calling its static values() method. 
  • All enum types get a static values() method automatically by the Java compiler. Here is an example of iterating all values of an enum:
 
  1. for (Day day : Day.values()) {
  2.     System.out.println(day);
  3. }

  • If you run this code, then you will get the following output:


  1. SUNDAY
  2. MONDAY
  3. TUESDAY
  4. WEDNESDAY
  5. THURSDAY
  6. FRIDAY
  7. SATURDAY


4.    Enum fields:


  • It is possible to add fields to Java Enum. Each constant enum value gets these fields. The field values must be supplied to the constructor of the enum when defining the constants. 
  • Here is an example:

  1. public enum Day {
  2.     SUNDAY(1), MONDAY(2), TUESDAY(3), WEDNESDAY(4), THURSDAY(5),
  3. FRIDAY(6), SATURDAY(7); 
  4.  
  5.     private final int dayCode;
  6.     private Day(int dayCode) {
  7.         this.dayCode = dayCode;
  8.     }
  9.  
  10. }
  • The example above has a constructor which takes an int. The enum constructor sets the int field. When the constant enum values are defined, an int value is passed to the enum constructor.
  • The enum constructor must be either private or package scope (default). You cannot use public or protected constructors for a Java enum.

5. Enum Methods:

  • It is possible to add methods to Java Enum. Here I am showing an example that how to do it.

  1. public enum Day {
  2.  
  3.     SUNDAY(1), MONDAY(2), TUESDAY(3), WEDNESDAY(4), THURSDAY(5),
  4. FRIDAY(6), SATURDAY(7); 
  5.  
  6.     private final int dayCode;
  7.  
  8.     private Day(int dayCode) {
  9.         this.dayCode = dayCode;    
  10.      }
  11.     public int getDayCode() {
  12.         return this.dayCode;
  13.     }
  14.     
  15. }

  • It is possible to call an Enum method through a reference to one of the constant values. The following code will show you how to call Java Enum method.
  • Day day = Day.MONDAY;
  • System.out.println(day.getDayCode()); //which gives 2.Because the dayCode field for the Enum constant MONDAY is 2.
 Some details to know about enum:

  • Java enums extend the java.lang.Enum class implicitly, so your enum types cannot extend another class. Please refer the following code. This code is from Java API.
  1. public abstract class Enum<E extends Enum<E>> implements Comparable<E>,Serializable {
  2. // Enum class code in Java API
  3. }
  • If a Java enum contains fields and methods, the definition of fields and methods must always come after the list of constants in the enum. Additionally, the list of enum constants must be terminated by a semicolon;
  • Java does not support multiple inheritance, and enum also does not support for multiple inheritance.
  • Enums are type-safe. Enum has their own name-space. It means your enum will have a type for example “Day” in below example and you cannot assign any value other than specified in Enum Constants.
  • Enum constants are implicitly static and final and cannot be changed once created.
  • Enum can be safely compare using:
  • Switch-Case Statement
  • == Operator
  • .equals() method
  • You cannot create instance of enums by using new operator in Java because constructor of Enum in Java can only be private and Enums constants can only be created inside Enums itself.
  • Instance of Enum in Java is created when any Enum constants are first called or referenced in code
  •  An enum can be declared outside or inside a class, but NOT in a method.
  • An enum declared outside a class must NOT be marked static, final , abstract, protected , or private
  • Enums can contain constructors, methods, variables, and constant class bodies.
  • Enum constructors can have arguments, and can be overloaded.
  • Enum constructors can have arguments, and can be overloaded.
  • The constructor for an enum type must be package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself

  1. package com.enum.exampleprogram;
  2.  
  3.  /**This is an Enum which indicates possible days in a week **/ 
  4.  
  5. public enum Day {
  6.     SUNDAY, MONDAY, TUESDAY, WEDNESDAY,THURSDAY, FRIDAY, SATURDAY;
  7. }


  1. package com.pamu.test;
  2. /** This class is an example of Enum  **/
  3. public class EnumTest {
  4.     Day day;
  5.     public EnumTest(Day day) {
  6.         this.day = day;
  7.     }
  8. //method to get descriptions of the respected days in a switch-statement.
  9.  
  10.  public void getDayDescription() {
  11.  
  12.  switch (day) {
  13.    case MONDAY:
  14.                System.out.println("Monday is First day in a week.");
  15.                 break;      
  16.   case FRIDAY:
  17.                 System.out.println("Friday is a partial working day.");
  18.                 break;        
  19.   case SATURDAY: 
  20.                 System.out.println("Saturday is a Weekend.");
  21.                 break;
  22.  case SUNDAY:
  23.                 System.out.println("Sunday is a Weekend.");
  24.                 break;               
  25.   default:
  26.                 System.out.println("Midweek days are working days.");
  27.                 break;
  28.         }
  29.     }
  30.     public static void main(String[] args) {
  31.  
  32.         EnumTest day1 = new EnumTest(Day.MONDAY);
  33.         day1.getDayDescription();
  34.  
  35.         EnumTest day2 = new EnumTest(Day.TUESDAY);
  36.         day2.getDayDescription();
  37.  
  38.         EnumTest day3 = new EnumTest(Day.WEDNESDAY);
  39.         day3.getDayDescription();
  40.  
  41.         EnumTest day5 = new EnumTest(Day.FRIDAY);
  42.         day5.getDayDescription();
  43.  
  44.         EnumTest day6 = new EnumTest(Day.SATURDAY);
  45.         day6.getDayDescription();
  46.  
  47.         EnumTest day7 = new EnumTest(Day.SUNDAY);
  48.         day7.getDayDescription();
  49.  
  50.     }
  51. }

Output:

  1. Monday is First day in a week.
  2. Midweek days are working days.
  3. Midweek days are working days.
  4. Friday is a partial working day.
  5. Saturday is a Weekend.
  6. Sunday is a Weekend.

Read: Enum in java part 2

Topic: Why Should You Do Microservices (or maybe you shouldn’t) Previous Topic   Next Topic Topic:

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use