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
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:
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
- Bir sakatlık ve yıkılan koskoca bir oluşum, Chicago Bulls #1
- DLive üzerinden yayın yapmak | İnternetten para kazanma yöntemleri #5
- Ev Stüdyosu Kurmak #2 Çekim alanı oluşturmak
- Ev Stüdyosu Kurmak #1 Basit kayıt işlemlerine başlamak / Doğru kamera seçimi
- Steemit üzerinde kazanç için sabır faktörünün önemi / İnternetten para kazanma yöntemleri #4
- Erman Baykan Unique Solo "Rain" w/ Shaman JPM Standart
- Steepshot ve Steembottracker kullanımı | İnternetten para kazanma yöntemleri #3
- SteemPress'den vote almak için neler yapmalı? İnternetten para kazanma yöntemleri #2
- İnternetten para kazanmak koca bir yalan mı? Bence değil | İnternetten para kazanma yöntemleri #1 Steemit
- Viki
- from Dream Theater with great memories. I signed those at the İstanbul concert in 2017.
- Spawn kimdir?
- Fight Club 2 / Çizgi Roman incelemesi
- Jaina & Anna
- Cúpula - Parte 1
- I DID IT!
- Liquid Tension Experiment / Liquid Trio Experiment
- ...ve Kral Lakers'da. NBA'de neler oluyor?
- Yazmak şeytanlarımızı yenmekte bize yardımcı oluyor
- Kubbe 2. Bölüm | İzmir'e varış