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

Ask A Question

Stats

  • Questions 100k
  • Answers 100k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Can you hook up a javascript debugger and check where… May 11, 2026 at 7:51 pm
  • Editorial Team
    Editorial Team added an answer You can use Action<T> for a delegate that takes a… May 11, 2026 at 7:51 pm
  • Editorial Team
    Editorial Team added an answer On my WinXP machine, I found my User Snippets at:… May 11, 2026 at 7:51 pm

Related Questions

The files residing in .settings in a Dynamic Web Project are: .settings/ |-- org.eclipse.jdt.core.prefs
Lets say I have a file t.txt, a directory t and another file t/t2.txt.
In Java 1.4.2 , class java.math.BigInteger implements interfaces Comparable , Serializable . In Java
I'm trying to get into Java web development but seem to be running into

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.