views:

25

answers:

2

I have two packages - x and y.

x contains the Student class and the Grade enum.

y contains the Klass class.

Why is the type Student.Grade.C not recognized in the Klass class in package y?

Do I need to define it in its own file and make it public?

package x;

enum Grade { A, B, C, D, F, INCOMPLETE };

public class Student {

// blah, blah, member variables, getters, setters, constructors    

}


package y;

public class Klass {

 // This enum type is not recognized in this package
 public static final MINIMUM_GRADE = Student.Grade.C; 

}
+4  A: 

Yes, you do have to declare that enum public. You shouldn't have to have it in its own file.

You would access just like your example Student.Grade.C;

You could import Student.Grade and just use C in your code.

Starkey
*`"Yes, you do have to declare that enum public. You shouldn't have to have it in its own file."`* -> If I make it **`public`**, I get the error: `"The public type Grade must be defined in its own file".`
Linc
Did you put the enum definition inside of the `Grade` class? If you put it outside (like your example) you need a separate file. If you want it outside, maybe you want a separate file... Is the enum coupled closely with `Grade` (it seems to be...)?
Starkey
Ah! That was it. Thanks, man!
Linc
+1  A: 

By not using public, protected or private, the Grade enum has the default access level - meaning only other classes in the same package can use it.

matt b