Dustin Sun
Dustin Sun

Reputation: 5532

What is wrong the Java enum definition

I defined public enum ABC in ABC.java and then compiled it to ABC.class.

In another XYZ.java, I use private ABC _abc.

XYZ and ABC are in some package.

But it tells cannot find symbol class ABC.

What is wrong?

package teacherII;

public enum Semester {

        Fall1999, Spring2000, Fall2000,
        Spring2001, Fall2001, Spring2002, Fall2002,
        Fall2003, Spring2004 
}

In the other file, I use

package teacherII;

public class Questionnaire {  
    private Semester _semester;

Compile command: javac -d ../classes/ Questionnaire.java

The Semester.class is at ../class/teacherII/Semester.class. It was there BEFORE I compile Questionnaire.java


Thank you guys so much for your reply. The season is that, as Chris and Vineet said, I didn't set classpath when compiling Questionnair. It works now. Thank you guys again!

Upvotes: 0

Views: 890

Answers (3)

danyim
danyim

Reputation: 1293

You may run into problems because you named your enum identifier the same as your class identifier (ABC), but if you're just simplifying for an example, then it should be fine.

To use ABC, you need to reference ABC as an instance of an object and then you will be able to access the enumerator.

For example...

public class ABC {
    public enum ABCEnum { ..., ..., ... };

...

}

public class XYZ {
    public static void main(String args[]) {
        ABC x = new ABC();
        System.out.println(x.ABCEnum);
    }
}

However, you can also make the enum static so that you don't need to do this..

public class ABC {
    public static enum ABCEnum { ... };

...

}

public class XYZ {
    public static void main(String args[]) {
        System.out.println(ABC.ABCEnum);
    }
}

Upvotes: 0

YoK
YoK

Reputation: 14505

There is no problem with your code.

In your case you get this error because you are using Enum in another class actually before compiling it.

Upvotes: 0

OscarRyz
OscarRyz

Reputation: 199215

It looks like you're not compiling correctly.

The definition of your enum looks ok. The reason you're getting that error message is because the compiled ( .class ) file is not present ( or reachable ) when you're trying to compile the second file.

So, for instance:

---- A.java ----
package a;
public enum A { one, two, three }
---- B.java ----
package a;
public class B {
   A x;
}

Will compile just fine with: javac A.java B.java

But it will fail, if for instance, you first compile B.java :

javac B.java  
B.java:3: cannot find symbol
symbol  : class A
location: class a.B
   A x;
   ^
1 error

I think something similar is happening to you.

So, basically check your classpath when compiling.

Upvotes: 3

Related Questions