Nerueger avatar

Printing elements in an Enum, How to learn Enum Elements' Indicator and defying identification to methods in Enum - Java

nerueger

Published: 22 Feb 2018 › Updated: 22 Feb 2018Printing elements in an Enum, How to learn Enum Elements' Indicator and defying identification to methods in Enum - Java

Printing elements in an Enum, How to learn Enum Elements' Indicator and defying identification to methods in Enum - Java

What Will I Learn?

  • Printing elements in an Enum
  • How to learn Enum Elements' Indicator
  • Defying ID to methods in Enum

Difficulty

  • Intermediate

Tutorial Contents


imagesource

Printing elements in an Enum

We can do the elements in the enum with the for loop instead of printing them one by one. But we'll use "foreach" instead for.

enum Cars
{
    AUDI ("Black") ,MERCEDES("White") ;
    public String color;
    Cars (String color)
    {
     this.color=color;
    }
}
public class Enum
{
    public static void main(String [] args)
    {
     for (Cars a:Cars.values())
     {
      System.out.println (a.name()+" - ") ;
      System.out.println (a.color) ;
     }
    }
}

What we'll see on the screen is:

AUDI - Black

MERCEDES - White

At the foreach, we were writing the type of the variable. But right here, our variables type is "Cars". Because it's an element in the type. By writing "Cars.values()", we mean the elements in the enum. After that, we print the enum datas with "a.name" and with "a.color", we print the datas in the parentheses. If we only write only "a" besides of "a.name", we would see the same result again. Because "a" is an element in the enum.

How to learn Enum Elements' Indicator

Enums are structurally similar to sequences and classes. Each element has an indicator and the first indicator starts at 0, as in the series.

Let's make an example and see how it works;

enum Cars
 {
   AUDI, MERCEDES;
 }
 public class Enum
  {
   public static void main(String []args)
    {
     System.out.println(Cars.AUDI.ordinal() );
     System.out.println(Cars.MERCEDES.ordinal());
    }
  }

What we'll see on the screen is:

0

1

So, we can see the enum elements indicator with "Ordinal ()" method.

We learned how to find the indicator of an element. Now, we'll make an example with using indicators and switch structures. Which means, enum elements can be in switch structures.

enum months
 {
  january, february, march // you do not have to put semicolon in this line
 }
 public class Enum {
  public static void main(String args[])
  {
   int x=months.march.ordinal (); // indicator has been received
   switch (x)
   {
    case 0:System.out.println("We're in the January");
    break;
    case 1:System.out.println("We're in the February");
    break;
    case 2:System.out.println("We're in the March");
    break;
    default:System.out.println("We're not in those months")
   }
  }
 }

What we'll see on the screen is:

We're in the March

We defined an enum called "Months" and defined elements. We have assigned the indicator of March to the variable x with ordinal method. I then compared it with the switch structure.

Defying ID to methods in Enum

enum Earnings{
    Allen (1000), Mark (1200), Scott(2000);
    private int earnings;
    Earnings(int m) {
     earnings=m
    }
    int showEarnings () {
        return earnings;
    }
    static void method2 ()
    {
     System.out.println("This is method2");
    }
}
public class Enum {
    public static void main(String args[])
    {
     System.out.println(Earnings.Allen.showEarnings () );
     System.out.println(Earnings.Mark.showEarnings () );
     System.out.println(Earnings.Scott.showEarnings () );
     Earnings.method2 ();
    }
}

What we'll see on the screen is

1000

1200

2000

This is method2

We defined 2 different methods in Enum. One of them is returning the earnings and other one writes a String on the screen. At the main method, we called those methods. For reaching to the method2 from inside the main, we defined it as static.

All Enums created from java.lang.Enum class.



Posted on Utopian.io - Rewarding Open Source Contributors

Leave Printing elements in an Enum, How to learn Enum Elements' Indicator and defying identification to methods in Enum - Java to:

Written by

blogs and vlogs about music, geek culture, nba and life

Read more #utopian-io posts


Best Posts From Nerueger

We have not curated any of nerueger's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Nerueger