Example of java enumerations: ValueOf, toString, Switch, compareTo

The type enum  in Java  is a special data type that allows you to declare constant variables. In Java, the declaration keyword for an enumerated type is enum.

Example of enum

We want to declare an enum variable with five search engines:

public enum searchengines{Google, Yahoo, Bing, Exhead, Baidu}; 
The comma at the end is optional, both writes are legal.
You can access the constants:

enginesSearch engine =  search engines.Google; 
All enumerations inherit from the class java.lang.Enum because a class can only inherit from one parent but from multiple interfaces.

The enumerated type is a powerful tool  since it defines a class or an interface that we can put methods and attributes in it. By default, the compiler adds methods such as the values() which returns an array containing the values in the order in which they are declared.

Browse with the for

The static method values() is useful in the case of a scan for example:

for(  Search Engines  Engine:  searchengines.values()) {
      System.out.println(engine);
}

Attributes and Methods

An enum type can contain constructors, methods, variables, and constants. You can initialize constants with the value that should be passed as an argument in a constructor.

public class EnumExample {

public enum searchengines{
Google(160), Yahoo(66.4f), Bing(7.f), Baidu(66), Ask(4.6f);
private float visitors;

private searchengines(float visitors) {
this.visitors = visitors;
}
}

public static void main(String[] args) {
for( search engines: searchengines.values()) {
System.out.println("Search engine: "+engine+"
visitors: "+engine.visitors+" millions");
}
}
}
Output:

Search engine: Google visitors: 160.0 million
Search engine: Yahoo visitors: 66.4 million
Search engine: Bing visitors: 7.0 million
Search engine: Baidu visitors: 66.0 million
Search engine: Ask visitors: 4.6 million
Constants are declared  static and final and cannot be changed once they are created.

The equality comparison between two enumerations

The comparison is done with the method equals() or with the operator '==' and both give the same result so you have the option to choose.

google search engines = searchengines.Google; 
if(google.equals(GoogleSearchengines))
System.out.println("comparison with equals");
if(google == searchengines.Google)
System.out.println("comparison with ==");

The Switch instruction

public class EnumExample{

public enum searchengines{
Google(160), Yahoo(66.4f), Bing(7.f), Baidu(66), Ask(4.6f);
private float visitors;

private searchengines(float visitors) {
this.visitors = visitors;
}
}

Searchengines name;

EnumExample(Searchengines name){
this.name=name;
}

public void slogan() {
switch (name) {
Google:
System.out.println("Don't be malicious, don't do evil");
estate;
box Yahoo:
System.out.println("Yahoo! It's You. Do you Yahoo?");
estate;
box Bing:
System.out.println("Bing is for doing.");
estate;
box Baidu:
System.out.println("Baidu knows Chinese better");
estate;
box Ask:
System.out.println("What's your question?");
estate;
}
}

public static void main(String[] args) {
Google Example = new ExampleNumber(SearchEngines.Google);
google.slogan();
Example enum yahoo = new Example Enum(Searchengines.Yahoo);
yahoo.slogan();
Example Num bing = new SampleNum(SearchEngines.Bing);
bing.slogan();
Baidu Example Enum = new Baidu Example Enum(SearchEngines.Baidu);
baidu.slogan();
EnumExample ask = new EnumExample(Searchengines.Ask);
ask.slogan();
}
}
You cannot create an instance of enum with the new keyword because the constructor is declared  Private  and is created during runtime. Even if you don't set it to private, it's still considered private.

toString and ValueOf

toString returns the name of the constant:

System.out.println(yahoo.toString());
The function valueOf() does the reverse processing of the method toString(), we give it the name and it returns the constant:

System.out.println(yahoo.valueOf("Yahoo")); 

compareTo

This function is used to compare two enumerations and returns -1, 0, 1 if this object is less, equal, or greater respectively.

System.out.println(google.compareTo(yahoo)); 
The console shows -1 because "google" is declared before "yahoo".

What to remember:
  • Easy declaration of constants and related values.
  • Enumerations  Inherits from the java.lang.Enum.
  • Enumerations cannot implement interfaces.
  • Enumerations  ><>
  • Variables and constructors are declared private.
  • Allow comparing two enumerations using the method compareTo().
  • Les  Enumerations  are used with the Switch.
References: