views:

647

answers:

5

I went to this interview for a software developer position and they gave me a test with some corner-case-code situations, with usually 4 options to choose.
One of the questions had an enum declared outside the class scope, I promptly checked the "does not compile" answer and went ahead with the other questions. It was something like:

enum Colors {BLUE,RED,GREEN}

class Test {
    //other code, not really important with my question
}

This code actually compiles.
Besides the fact that an interview like this (might or) might not be useful to find out if one is a good developer, what worries me is: why would I declare an enum like this? Why I can only do this with enum? I did some testing and found out that it is visible inside the class, but not to other classes.

Sidenote: I scored really poor :P. I got the max on the theory but near the lowest possibile on the corner-case-code situations. I don't think I'll get the job.

A: 

It compiles actually, on my Eclipse ! ;-)

Several classes are allowed to be in the same file. The limitation is that a public class has to be defined in a file that has the same name.

It's visibility is 'package', so it should be visible in other classes in the same package too.

What can I do with that enum?

You can do anything you want with the above limitations...

Note : although you had it wrong, you shouldn't feel too bad, because it's not really a good practice either. In our CheckStyle configuration, outer classes in the same file like this are treated as errors !!

KLE
of course it compiles. I think the OP realized that it compiles (but wasn't clear about it)
newacct
Yes, I know, I should clarify it in my question.
Alberto Zaccagni
+4  A: 

It's not just enums. Enums are just special kinds of classes. In general you can have multiple classes declared in one file (as long as no two of them are public).

newacct
And the class file name can be freely chosen (e.g. MyClass.java for the example).
Thomas Jung
I totally missed the passage "Enums are just special kinds of classes", with that in mind this starts making sense.
Alberto Zaccagni
Actually this works for classes (not just enums).
Steve Kuo
+2  A: 

No, without an access modifier, the enum is package-private. This means it can only be used by classes in the same package. And you can't only do this with an enum, classes can also be made package-private.

More info: http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

Bart Kiers
Ignoring the long arm of Reflection.
kenny
@Kenny, very true! ;)
Bart Kiers
+1  A: 

Sometimes this idiom can be sensible - for example, imagine you have an UploadHandler class (or something like that) which can return a status from an upload. It seems quite feasible to me to implement this status as an enum - and since the enum (e.g. UploadStatus) clearly "belongs" to the UploadHandler class, it seems fine to declare it in the same source file. (This does assume of course that it only needs to be package-private - if it's truly public it would need to be declared in its own file, which would probably make sense if it's not an internal thing any more).

As it happens, in this case I would probably make it a static inner class to make the relationship more explicit. But declaring multiple classes in the same source file isn't always bad and can sometimes help readability by setting the expectation that this is a borderline-trivial, subsidiary class. (By the same token, I don't think classes like this should do anything particularly complex or unexpected.)

Andrzej Doyle
A: 

An enum specifies a list of constant values that can be assigned to a particular type. It can be either inside or outside of the class.

Madhu