Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at diehardtechy@gmail.com

Tuesday, July 15, 2014

Understanding Enum: Definition And Need Of Enum : Chapter 2

Before you read this tutorial we suggest you to first read Understanding Enum: Definition And Need Of Enum : chapter 1 

Every enum inherits methods from java.lang.Enum class. The important methods are

public final String name()
Returns name of the current enum exactly as declared in its enum declaration.

Public final int ordinal()
Returns current enum position.

 
Write a program to print name and ordinal of all enum objects of enum Months. 

//Months.java

enum Months
{
             JANUARY,FEBRUARY,MARCH,APRIL,MAY,
             JUNE,JULY,AUGUST,SEPTEMBER,OCTOBER,NOVEMBER,DECEMBER;
}

//MonthsNameAndOrdinal.java

class MonthsNameAndOrdinal
{
             public static void main(String[] args)
             {
                             Months []months=Months.values();

                             for(Months month:months)
                             {
                                             System.out.print(month.name());
                                             System.out.print("....");
                                             System.out.print(month.ordinal()+"\n");
                                            
                             }
             }

Q? How can we assign prices (values) to Menu items in enum ? Is below syntax is valid ?

enum Months
{
             JANUARY=1, FEBRUARY=2;
}

It is a wrong syntax, it leads to CE because named constants are not of type “int” they are of type “Months”. So we can not assign values to named constants directly using “=” operator.

Then how can we assign?

Syntax:

             Namedconstant (value)

             For example: JAN(1), FEB(2)

Rule: To assign values to names constants as shown in the above syntax, enum must have a parameterized constructor with the passed argument type. Else it leads to CE.


Q. )Where these values 1, 2 are stored?

A: ) We must create a non-static int type variable to store these values. So, to store these named constants values we must follow 3 rules in enum class.

·         We must create non-static variable in enum with the passed argument type.
·         Also we must define parameterized constructor with argument type.
·         Named constants must end with “;” to place normal variables, methods, constructor explicitly. It acts as separator for compiler to differentiate named constants and genegarl members. 


Below code shows defining enum constants with values.

//Months.java

enum Months
{
                JAN(1),FEB(2);
                private int num;
                Months(int num)
                {
                                this.num=num;
                }
                public int getNum()
                {
                                return num;
                }
                public void setNum(int num)
                {
                                this.num=num;
                }
}  


Write a program to print above Months as how the real Months items are appeared.

Expected output:
1. JAN
2. FEB

//Year.java

class Year
{
                public static void main(String [] args)
                {
                                Months [] month=Months.values();
                                for(Months mon:month)
                                {
                                                System.out.print(mon.getNum()+". ");
                                                System.out.println(mon.name());
                                }
                }
}



No comments: