Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum declared outside class scope

Tags:

java

enums

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.

like image 309
Alberto Zaccagni Avatar asked Oct 01 '09 10:10

Alberto Zaccagni


People also ask

Can an enum be outside a class?

Enums can be declared outside the class or inside the class however they must not be declared inside the method.

How do you call an enum that is outside of a class?

You have to declare enum as public ... (depend on what you want) and then use class name as prefix when outside package. @RongNK - It doesn't have to be public ; if its in the same package, it can have default access. You need to use the class name as a prefix even within the same package unless you import the enum.

Should enum be in class or outside?

If only your class members use the enum it is preferable to declare the enum inside the class. It is more intutive for users of the class, it helps the user to know that the enum will only be used by the class.

Can you declare an enum in a class?

Yes, we can define an enumeration inside a class. You can retrieve the values in an enumeration using the values() method.


3 Answers

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).

like image 135
newacct Avatar answered Oct 17 '22 03:10

newacct


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

like image 28
Bart Kiers Avatar answered Oct 17 '22 03:10

Bart Kiers


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.)

like image 44
Andrzej Doyle Avatar answered Oct 17 '22 03:10

Andrzej Doyle