views:

139

answers:

3

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!

A: 

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);
    }
}
danyim
You forgot to change the second example. `x.ABCEnum` should read `ABC.ABCEnum`.
Mike
The example is wrong: According to the question, the ABC enum is not an inner class inside the ABC class, it's just an enum.
Jorn
Thanks, Mike. I made the changes. Jorn--I meant to show that ABC and XYZ are separate files, which is what the original post implied.
danyim
+3  A: 

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.

OscarRyz
just a precision, you may compile B after you compiled A but you must set the classpath to the directory where A.class is located;something like: `javac -cp path_to_a B.java`
Chris
A: 

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.

YoK