2

If I have an Enum as a helper in a Java class, is there any way to refer to that Enum outside of the class it's helping?

Basically, what I have is this:

class Account extends MyClass {
    HashMap<Property, String> property = new HashMap<Property, String>();
    public Account() {
    }

    public enum Property {
        USERID,
        PASSWORD;
    }
}

I want to be able to access the Property enum outside of the Account class.

The reason I want to do this is because this is a subclass of a another, and I want to be able to access the properties of a given subclass without referring to a unique enum name (i.e.: without referring to each one as, say, AccountProperty or ResearchProperty or TaskProperty etc).

informatik01
  • 16,038
  • 10
  • 74
  • 104
Richard Rhyan
  • 203
  • 1
  • 6
  • 17

3 Answers3

3

Your enum is public so you just can use Account.Property to access it from outside the Account class

EDIT :

If I got what you need, you'd like to do something like

Property p = Account.Property.PASSWORD;
Property p1 = Product.Property.CODE;

where Product is

public class Product extends MyClass{
    HashMap<Account.Property, String> property = new HashMap<>();
    public Product() {
    }

    public static enum Property {
        CODE,
        PRICE;
    }
}

and you want to do this in your MyClass.

The problem is that both the two lines require an import and you can't import two classes with the same name, so the only solution is to do something like this

Account.Property p = Account.Property.PASSWORD;
Product.Property p1 = Product.Property.CODE;

I guess that you've got to deal with the instanceof to use the right Property enum for each class, as there's no way to extend an enum!

StepTNT
  • 3,867
  • 7
  • 41
  • 82
  • Is this possible to access it statically if the enum is a member, not static class? – User May 19 '12 at 16:18
  • I don't understand your question ! Maybe you can add a sample code of what you're trying to do :) – StepTNT May 19 '12 at 16:21
  • I'm not trying to do anything, I'm just asking if what you are saying is possible (can't test it now). Property is an inner non static class of Account so I'm wondering why it's possible to access it in a static way (Account.Property) instead of accountInstance.Property – User May 19 '12 at 16:23
  • 2
    As said here http://stackoverflow.com/questions/253226/nested-java-enum-definition-does-declaring-as-static-make-a-difference , _Nested enum types are implicitly static._ That's why it works and you don't need to declare it static – StepTNT May 19 '12 at 16:26
  • I did find out that I could use Account.Property. But I'd like to, if at all possible, use something more like MySuperClass.Property, so that any of the subclasses could use it. Or perhaps Property p = Account.Property so that I can use other Property enums in the same code. – Richard Rhyan May 19 '12 at 16:34
  • I still don't get what's your real problem. You want the subclasses of `Account` to use your `Property` enum? – StepTNT May 19 '12 at 16:44
  • Nope. I want different subclasses of MyClass (I just edited the post to show that, sorry) to have different Property enums, which can be access from outside the main class, possibly with something like: enum p = Account.Property; or enum p = Task.Property; and the "p" could be used in place of specifying Account.Property or Task.Property – Richard Rhyan May 19 '12 at 16:48
  • Basically, I'm going to have about 25 subclasses, and I'd rather not use a series of if (... instanceof ...) for setting and getting variables. – Richard Rhyan May 19 '12 at 16:50
  • I've edited my answer, hoping that I finally understood what you need! – StepTNT May 19 '12 at 17:02
  • I think so yes... It appears what I want to do can't be done. – Richard Rhyan May 19 '12 at 17:08
  • Nope. It can't. Nested enums are _always_ static. – Louis Wasserman May 19 '12 at 18:10
1

maybe something like the following (but this has no type checking):

import java.util.*;
abstract class MyClass {
    Map<Object,String> properties=new HashMap<Object,String>();
}
class Account extends MyClass {
    enum Property {
        userid,password
    }
    //static Set<Property> keys=EnumSet.allOf(Property.class);
}
class Research extends MyClass {
    enum Property {
        red,green;
    }
    static Set<Property> keys=EnumSet.allOf(Property.class);
}
public class So10666881 {
    public static void main(String[] args) {
        Account account=new Account();
        account.properties.put(Account.Property.userid,"user");
        account.properties.put(Account.Property.password,"pass");
        for(Account.Property property:Account.Property.values())
            System.out.println(property+"="+account.properties.get(property));
    }
}
Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
0

Just declare the enum as a public top level enum class (in its own file)

raphaëλ
  • 6,393
  • 2
  • 29
  • 35
  • But I'm trying to use different Property enums using the same code. This will only work with a single Property enum. – Richard Rhyan May 19 '12 at 16:35
  • But those will be different enum types. In the java language you can't subclass enums. What is your actual problem at hand? – raphaëλ May 19 '12 at 16:37