Java examples on Java Basic
- What's needed to be prepared for learning Java programming?
- How do I write Hello World program in Java?
- Java Programming Keywords Summary
- How do I get the command line arguments passed to the program?
- How do I use Override annotation?
- How do I create an inner class?
- How do I clone an object?
- How do I mark method as deprecated?
- How do I do bitwise exclusive OR operation?
- What is SuppressWarnings annotation?
- How do I compare string regardless their case?
- How do I define constructor in enum type?
- How do I create type specific collections?
- How do I use enum in switch statement?
- What is reference variable in Java?
- How do I get ordinal value of enum constant?
- How do I get the remainder of a division?
- How do I get name of enum constant?
- How do I do bitwise AND operation?
- How do I use the ternary operator?
- How do I get enum constant value corresponds to a string?
- How do I create custom exception class?
- How do I use static import feature?
- How do I do bitwise OR operation?
- How do I count the occurrences of a number in an array?
- How do I use the boolean negation (!) operator in Java?
- How do I use the if-then-else statement?
- How do I use the final keyword in Java?
- How do I define a field in enum type?
- How do I create a class in Java?
- How do I use the for loop statement?
- How do I use the return keyword in Java?
- How do I use the this keyword in Java?
- How do I use the super keyword?
- How do I convert double value into int value?
- How do I create enumerations type?
- How do I use the while loop statement?
- How do I use the do-while loop statement?
- How do I define a constant variable?
- How do I get constants name of an enum?
- How do I use the throws keyword to declare method exceptions?
- How do I implement interfaces in Java?
- How do I create static variables in Java?
- How do I catch multiple exceptions?
- How do I create and implement abstract class?
- How do I use the switch statement?
- How do I throw exceptions in Java?
- How do I use the && operator in Java?
- How do I use string in switch statement?
- How do I extend classess in Java?
- How do I create an interface in Java?
- How do I use try-with-resources statement?
- How do I overload methods in Java?
- How do I override a method in Java?
- How do I use the instanceof keyword?
- How do I use the unary bitwise complement "~" operator?
- How do I use the continue statement?
- How do I pick a random value from an enum?
- How do I use the break statement?
- How do I use checked and unchecked exception?
- How do I use the relational operator in Java?
- How do I create constructors for a class?
- How do I declare and initialize local variable?
- How do I use the || operator in Java?
- How do I invoke superclass constructor?
- How do I use the equality operator in Java?
- How do I use the double brace initialization?
- How do I use the if-then statement?
- How do I handle exceptions using try-catch block?
- How do I declare and initialize variable?
- How do I use the arithmetic operators?
- How do I use the diamond syntax?
- How do I use the shift left "<<" operator?
- How do I use the shift right ">>" operator?
- How do I use the unary operators?
- How to use underscore in numeric literals?
- How to I define an integer constant in binary format?
How do I get ordinal value of enum constant?
This example demonstrate the use of enum ordinal() method to get the ordinal value of an enum constant.
package org.kodejava.example.fundamental;
enum Color {
RED, GREEN, BLUE
}
public class EnumOrdinal {
public static void main(String[] args) {
//
// Gets the ordinal of this enumeration constant (its
// position in its enum declaration, where the initial
// constant is assigned an ordinal of zero)
//
System.out.println("Color.RED : " + Color.RED.ordinal());
System.out.println("Color.GREEN: " + Color.GREEN.ordinal());
System.out.println("Color.BLUE : " + Color.BLUE.ordinal());
}
}
The program print the following result:
Color.RED : 0 Color.GREEN: 1 Color.BLUE : 2