Tuesday, July 5, 2011

what is use of ordinal method of Enum class?

ProtoType of method is following .
public final int ordinal()


Method Name :ordinal
Return Type :int


It is final so no one override this method


Doc Specification :
/** * Returns the ordinal of this enumeration constant (its position * in its enum declaration, where the initial constant is assigned * an ordinal of zero). * * Most programmers will have no use for this method. It is * designed for use by sophisticated enum-based data structures, such * as {@link java.util.EnumSet} and {@link java.util.EnumMap}. * * @return the ordinal of this enumeration constant */


Source Code :



package model;


public class Simple {
    public Simple() {
        super();
    }
enum Hello{     H1,H2;         }     public static void main(String[] args) {        Hello aa=Hello.H2;
        int i=aa.ordinal();
        System.out.println(i);
    }
}


the out put is :1
it just return the position of any enum variable .it starting point is zero same as Array .