Java Programming | Using Enumeration

Introduction.

This is a tutorial for enumeration in java. The Enumeration is a set of constants. Its very easy. So Lets See it…

Program for Enumeration in Java.

// declare enum
enum no
{
    //constants
    one(1),
    two(2),
    three(3);
 
    int n; //integer varible
    //constructor
    no(int o)
    {
    n = o;
    }
 
    //get value
    int getvalue()
    {
      return n;
    }
}
 
// the name of our class its public
public class EnumExample {
    //void main
        public static void main (String[] args)
        {
            //print the value of constants
            System.out.println("One is "+no.one.getvalue());
            System.out.println("Two is "+no.two.getvalue());
            System.out.println("Three is "+no.three.getvalue());
        }
}

Output

One is 1
Two is 2
Three is 3

How does it work

No need to input, directly prints the constants.

Extending it

The program can be extended. This is a basic concept of java programming and has lots of applications.

Explanation.

  1. Declare the enum.
  2. Declare the class as public
  3. Add the void main function
  4. Add system.out.println() function to print the constants.

At the end.

You learnt creating the Java program for Using enumeration. So now enjoy the program.

Please comment on the post and share it.


Sharing options: