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 96025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:45:00+00:00 2026-05-10T23:45:00+00:00

In java <1.5, constants would be implemented like this public class MyClass { public

  • 0

In java <1.5, constants would be implemented like this

public class MyClass {     public static int VERTICAL = 0;     public static int HORIZONTAL = 1;      private int orientation;      public MyClass(int orientation) {         this.orientation = orientation;     } ... 

and you would use it like this:

MyClass myClass = new MyClass(MyClass.VERTICAL); 

Now, in 1.5 obviously you should be using enums:

public class MyClass {     public static enum Orientation {         VERTICAL, HORIZONTAL;     }      private Orientation orientation;      public MyClass(Orientation orientation) {         this.orientation = orientation;     } ... 

and now you would use it like this:

MyClass myClass = new MyClass(MyClass.Orientation.VERTICAL); 

Which I find slightly ugly. Now I could easily add a couple of static variables:

public class MyClass {     public static Orientation VERTICAL = Orientation.VERTICAL;     public static Orientation HORIZONTAL = Orientation.HORIZONTAL;      public static enum Orientation {         VERTICAL, HORIZONTAL;     }      private Orientation orientation;      public MyClass(Orientation orientation) {         this.orientation = orientation;     } ... 

And now I can do this again:

MyClass myClass = new MyClass(MyClass.VERTICAL); 

With all the type-safe goodness of enums.

Is this good style, bad style or neither. Can you think of a better solution?

Update

Vilx- was the first one to highlight what I feel I was missing – that the enum should be a first-class citizen. In java this means it gets its own file in the package – we don’t have namespaces. I had thought this would be a bit heavyweight, but having actually done it, it definitely feels right.

Yuval’s answer is fine, but it didn’t really emphasise the non-nested enum. Also, as for 1.4 – there are plenty of places in the JDK that use integers, and I was really looking for a way to evolve that sort of code.

  • 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. 2026-05-10T23:45:01+00:00Added an answer on May 10, 2026 at 11:45 pm

    Don’t know about Java, but in .NET the good practice is to put enums in parallel to the class that uses them, even if it is used by one class alone. That is, you would write:

    namespace Whatever {     enum MyEnum     {     }     class MyClass     {     } } 

    Thus, you can use:

    MyClass c = new MyClass(MyEnum.MyValue); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, I am currently trying to learn some Java programming. To do this I'm
I need to implement an ArrayList which can hold like person records. I can
I'm developing a Java enterprise application, currently doing Java EE security stuff to restrict
Sorry for the very involved question, but this is something I've been researching for
I've been comparing a STL implementation of a popular XmlRpc library with an implementation
I'm going to implement a tokenizer in Python and I was wondering if you
Are there any open source or commercial tools available that allow for text fragment
I am creating a tool that will check dynamically generated XHTML and validate it
If all that you're doing is a simple one-pass iteration (i.e. only hasNext() and

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.