12

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.

Denys Séguret
  • 335,116
  • 73
  • 720
  • 697
Alberto Zaccagni
  • 28,473
  • 10
  • 71
  • 102
  • 3
    It will be visible to other classes in the same namespace, not only within a file. Most enums in BCL are declared outside of a class, just like the example you provided (except that they are public). So the question is why *wouldn't* you declare an enum like this? – Groo Oct 01 '09 at 10:17
  • It's useful to check if someone worked with the java access control and classes are not 100% of the form public class A{private String field; public method(){}} by default. – Thomas Jung Oct 01 '09 at 10:49

5 Answers5

11

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
  • 110,405
  • 27
  • 152
  • 217
6

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
  • 153,868
  • 34
  • 276
  • 272
1

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
  • 97,637
  • 30
  • 185
  • 225
0

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
  • 22,211
  • 4
  • 51
  • 60
0

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
  • 5,306
  • 8
  • 33
  • 50