2

Is it possible in Java to return the enum value without having to call a function to return the value, such as getFlag() in my example? If so, how?

public enum MessageFlags {
        BIT0((short)1),
        BIT1((short)2),
        BIT2((short)4),
        BIT3((short)8),
        BIT4((short)16),
        BIT5((short)32),
        BIT6((short)64),
        BIT7((short)128),
        BIT8((short)256),
        BIT9((short)512),
        BIT10((short)1024),

        set_freq(BIT0),
        get_freq(BIT1);

        short bitFlag = 0;
        MessageFlags flag;

        MessageFlags(short flag) {
            this.bitFlag = flag;
        }

        MessageFlags(MessageFlags flag) {
            this.flag = flag;
        }

        public short getFlag() {
            return this.flag.bitFlag;
        }

        public short getValue() {
            return this.bitFlag;
        }
    }
6
  • No offense, but why does it matter whether it's direct or a method call? Commented Aug 11, 2011 at 19:08
  • @Daniel: Because it crowds my if statements. Commented Aug 11, 2011 at 19:09
  • I'm confused. Why does a MessageFlags have a MessageFlags field called flag? Commented Aug 11, 2011 at 19:14
  • @Kublai: Because I am constructing set_flag(BIT0) which is of type MessageFlags. It is more verbose in my if statement than saying MessageFlags.BIT0.getFlag() and I can say MessageFlags.set_freq.getFlag() Commented Aug 11, 2011 at 19:16
  • 1
    @Code: It seems like you have a design issue with set_freq. Why is set_freq one of the enumerables? Commented Aug 11, 2011 at 19:29

4 Answers 4

3

Just say MessageFlags.BITX and that will return the same value as getFlag()

Sign up to request clarification or add additional context in comments.

Comments

2

You can import static MessageFlags.*; and say BITX.getFlag().

Here is a complete example:

A.java

package foo;

import static foo.B.*;

public class A{
    public B value = BAR;
}

B.java

package foo;

public enum B{
    BAR, BAZ, BOO
}

8 Comments

I like this way better than the one I suggested.
"import MessageFlags cannot be resolved" - that is the error I get.
@Code, did you use the proper package name? Did you use the word "static"?
Yes, used static. No package name is defined at the top of my MessageFlags.java file (which contains the enum).
Then I suggest you put it in a package. You cannot import a class that is not in a package.
|
1

I followed @Jeremy's advice of this:

package foo;

import static foo.B.*;

and then I created a method called set_freq in my MessageFlags enum. I made this function static and had it return short. For example,

public static short set_freqflag() {
    return BIT0.getFlag();
}

The semantics of set_freqflag are a little weird because you are not setting anything but I do not have a better name at the moment. This allows me to just state set_freqflag() rather than the longer way I was doing before.

Comments

0

I might be really late, but I'm writing to anyone who visits this topic for help.

If you have an enum, and you'd like to return a specific parameter of its values by default without calling a get function, you need to insert an @Override above your selected function, like:

public class Plants {
  public enum Fruits {
    APPLE ("sweet"),
    GRAPEFRUIT ("sour");

    private String taste;

    Fruits (String taste) {
      this.taste = taste;
    }

    @Override
    public String getTaste() {
      return this.taste;
    }
  }
}

And now you can call whichever enum value you'd like, without a get function:

Plants.Fruits.APPLE

And it'll return "sweet"

P.S. I'm not a professional programmer, please correct me if I've written something anti-conventional by accident.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.