tags:

views:

465

answers:

5

I have 2 files which are interacting with each other. I wanted to define an enum to make the code more readable, but if I define it in file 1, file 2 complains about having no knowledge of said enum. If I define ii in file 2, file 1 does the same. I am defining it as public too.

The solution was to define the enum in both files, but this doesn't seem right to me. Not only is it redundant, but I fear it may cause some conflict, even if the types have the same items.

What is the veredict on this? Am I doing something wrong or worrying too much?

EDIT

Well, given the comments here I found an alternative which seems to be doing what I want without having to create a new file. I had:

file 1

class myClass1
{
    public enum MyEnum
    {
        ...
    }
    ...
}

file 2

class myClass2
{
    public enum MyEnum
    {
        ...
    }
    ....
}

Now, I have:

file 1

enum myEnum
{
    ...
}

...

class myClass1
{
     ...
}

file 2

class myClass2
{
     ...
}

I didn't want to create another file just for the enum, so this works for me. Well, as long as there is nothing wrong with it, which I think there isn't.

+4  A: 

You definitely shouldn't define the enum in both locations. I recommend defining the enum in its own file with public accessibility. Then everyone should have no trouble accessing it. However, assuming that you want to define the enum in one of the two classes, I'll continue...

You have to import the enum or use its fully qualified name. Assuming you are in the package com.stackoverflow, your classes should look like this, in the first file:

package com.stackoverflow;

public class A {
    public enum MyEnum {
      ONE,TWO,THREE;
    }

    ...
}

and in another file:

package com.stackoverflow;

import com.stackoverflow.A.MyEnum;

public class B {

  public void test(MyEnum mine) {
    ...
  }

  ...
}
Eddie
Alternatively you can write "import com.stackoverflow.A;" and "test(A.MyEnum mine)"
Esko Luontola
Although in that example A and B are in the same package, so imports are not needed. Just write "A.MyEnum".
Esko Luontola
Shouldn't MyEnum be declared static?
Outlaw Programmer
@Outlaw Programmer: See http://stackoverflow.com/questions/253226/nested-java-enum-definition-does-declaring-as-static-make-a-difference
Eddie
@Esko Luontola: Yes. I was being thorough for the case where they are in different packages, but for the code sample I provided you're exactly correct.
Eddie
+3  A: 

No - it is wrong. The enum can be declared as a static inner class and referenced using the fully-qualified name

public class MyClass {
    public static enum MyEnum { BOOK, DVD }


    public void myMethod() {
        MyEnum e = MyEnum.DVD;
    }
}


public class B {
    public void someMethod() {
        MyClass.MyEnum enumVal = MyClass.MyEnum.BOOK;
    }
}
oxbow_lakes
Enums are implicitly static when they are declared as member classes, the same way as interfaces are implicitly static, so you don't need to use the 'static' keyword. Just have it public.
Esko Luontola
Yes - I added the static Keyword in one of my edits; thought it might be a bit clearer to have it explicit
oxbow_lakes
A: 

You define your enums the same way you define classes - either in their own file, or as static inner classes (they'd have to be static not to be bound to their enclosing class).

Defining your enum in 2 files is broken, as it's sort of defeating the purpose - you'll have 2 separate enums, and while you'll want to test equality on Enum.A you'll actually have Enum1.A !=Enum2.A. Very confusing.

THere's no real mystery to them, it's just a nicety around a way to define constants that's safer and more capable than public static final.

Steve B.
A: 

It seems like yours is a problem of access modifiers.

Take a look at this link

OscarRyz
+1  A: 

Also remember when u put enum in another class's file you risk the problem of unable to access the enum from another package due to default access privilege. ... so It is always wise to put the enum in a separate file with public access or as a public static member of a class that can be accessed with the class name.