Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9292059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:47:22+00:00 2026-06-18T20:47:22+00:00

I notice that the Java Enum Documentation states the ordinal method: Returns the ordinal

  • 0

I notice that the Java Enum Documentation states the ordinal method:

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 EnumSet and EnumMap.

I understand all the examples online that suggest not to use ordinal to index into an array but an EnumMap instead. Particularly Item 33 of Effective Java
But my question is: is it ok to use it within my Enum definition?
For example my code is as follows:

public enum Direction {
   NORTH(0, 1), NORTH_EAST(1, 1), EAST(1, 0), SOUTH_EAST(1, -1),
   SOUTH(0, -1), SOUTH_WEST(-1, 1), WEST(-1, 0), NORTH_WEST(-1, 1);

   private final int xOffset;
   private final int yOffset;

   private final static int DEGREES = 360;

   private Direction(int xOffset, int yOffset) {
     this.xOffset = xOffset;
     this.yOffset = yOffset;
   }

  public Position move(Position position) {
     return new Position(position.getX() + xOffset, position.getY() + yOffset);
  }

  public Direction rotate(int degrees) {
     int length = Direction.values().length;
     int index = (ordinal() + (degrees / (DEGREES / length))) % length;
     return Direction.values()[index];
  } 
}

As you can see I’m using ordinal() so that I can cycle through the directions (and return the next relevant direction). E.g. rotating 90 degrees from NORTH will return EAST.

However I know that using ordinal isn’t good practice so I wondered if there is a better way to do this whilst still keeping the code size down and maintaining readability.

I’d be very appreciative for any advice!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T20:47:24+00:00Added an answer on June 18, 2026 at 8:47 pm

    I’d say this is a perfectly acceptable use of ordinal. In fact it is, in my opinion, quite neat and simple.

    The comments about not using ordinal in your enum code is merely to discourage you from doing so because most of the time you can use the enum itself or an EnumMap instead.

    Your reference to EffectiveJava is warning you against doing this because choosing the correct int to use as offset to an array is fraught and generally unnecessary. In your case that doesn’t apply because a) You are doing it correctly and b) It is the simplest method to achieve the functionality you are looking for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

am working on a java(tomcat) app. that sometimes writes to stdout. But I notice
I notice that Java has a number of ancillary scripting languages. Clojure and Groovy
<HTML> <BODY> <% // This is a scriptlet. Notice that the date // variable
I notice that a number of Java exception classes differ only in the name
I would like to create an initialisation method for a Java class that accepts
I'm trying to create some XML like this: <parameter name=srid type=java.lang.Integer>24729</parameter> notice that the
I notice that a snapshot of the Java 7 API has been up on
I'm migrating from Java 1.1. to Java 5. I notice that some methods have
I was reading Oreilly Java Programming With Oracle JDBC edition and I notice that
We began to notice that with Java 7 (particularly with update 4), that all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.