A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/lang/java_lang_enum.htm below:

Java - Enum class

Java - Enum class Introduction

The Java Enum class is the common base class of all Java language enumeration types.

Class Declaration

Following is the declaration for java.lang.Enum class −

public abstract class Enum<E extends Enum<E>>
   extends Object
      implements Comparable<E>, Serializable
Class constructors Sr.No. Constructor & Description 1

protected Enum(String name, int ordinal)

This is the single constructor.

Class methods Methods inherited

This class inherits methods from the following classes −

Example

Following example showcases the usage of enum in if and switch statements.

package com.tutorialspoint;

public class EnumDemo {
   public static void main(String args[]) {
      
      //print an Enum
      System.out.println(Mobile.Motorola);

      Mobile mobile = Mobile.Samsung;
      //Usage in IF statment
      if(mobile == Mobile.Samsung) {
         System.out.println("Matched");
      }
      //Usage in Swith statment
      switch(mobile) {
         case Samsung:
            System.out.println("Samsung");
            break;
         case Nokia:
            System.out.println("Nokia");
            break;
         case Motorola:
            System.out.println("Motorola");
      }
   }
}
enum Mobile {
   Samsung,
   Nokia,
   Motorola
}
Output

Let us compile and run the above program, this will produce the following result −

Motorola
Matched
Samsung

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4