3.2 Declaring Enums Inline
While it's useful to create a
separate enum class, defined in its own
source file, sometimes its also useful to just define an enum, use it, and
throw it away. This is possible through member types.
3.2.1 How do I do that?
Just define the enum within your class, as you would any other member
variable. You might
need a DownloadStatus enum, for example, but only
within a Downloader class:
Oddly enough, this same code may be written as follows:
public class Downloader {
public enum DownloadStatus { INITIALIZING, IN_PROGRESS, COMPLETE };
// Class body
}
In this case, the static modifier has been added. This has no effective
public class Downloader {
public static enum DownloadStatus { INITIALIZING, IN_PROGRESS, COMPLETE };
// Class body
}
change on the enum, as nested enums are implicitly static. In other
words, it's sort of like declaring an interface abstractit's redundant.
Because of this redundancy, I'd recommend against using the static
keyword in these declarations.NOTEExamples in the
enum specification
also omit
"static" in nested
declarations.