Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why enums only on a top level class or interface? [duplicate]

Tags:

java

enums

I am studying Java and wrote a very simple program. In it I can put enums in the top most level, but not inside a method. The way I see it, enums are almost like constants, so why not use them inside methods?

In my program, enum1 is allowed, but enum2 is not. Why?

enum enum1 {A, B, C};

public static void main(String[] args)
{
    enum enum2 {A, B, C};  // only on a top level class or interface
}
like image 474
Cindy Langdon Avatar asked Jun 17 '14 20:06

Cindy Langdon


People also ask

Can enum be an inner class in Java?

You can't have a static nested type inside a non-static one (a.k.a an "inner class", see JLS §8.1. 3. Inner Classes and Enclosing Instances). Therefore you can't have an enum inner type inside a non-static nested type.

What is difference between class interface and enum?

Interfaces are designed to define common behaviours, enums to define common values. Enum represents a real value which can be compared to another value, or stored in the database easily.

Can we declare enum inside class?

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

Can we define enum inside interface?

Yes, Enum implements an interface in Java, it can be useful when we need to implement some business logic that is tightly coupled with a discriminatory property of a given object or class. An Enum is a special datatype which is added in Java 1.5 version.

Can We declare enums and classes inside an interface?

This small article provides an outline of the usage of classes and enums inside an interface that seem to be unusual to a developer. I recently was interviewing for candidates and I asked the basic question of whether we can declare enums and classes inside an interface. Most candidates said "NO" but the answer is "Yes" .

What is an enum in Java?

An enum is a special "class" that represents a group of constants (unchangeable/read-only variables). To create an enum, use the enum keyword (instead of class or interface), and separate the enum items with a comma: Enum is short for "enumerations", which means "specifically listed".

Why can't enums have static methods?

Because enums are a special type of class definition. This doc describes some information about special things the compiler does including adding static methods (valueOf). While you could declare an anonymous class withing the context of a method, it could not have static methods.

What is enum class in C++11?

C++11 has introduced enum classes (also called scoped enumerations ), that makes enumerations both strongly typed and strongly scoped. Class enum doesn’t allow implicit conversion to int, and also doesn’t compare enumerators from different enumerations. To define enum class we use class keyword after enum keyword.


2 Answers

Because enums are a special type of class definition.

This doc describes some information about special things the compiler does including adding static methods (valueOf). While you could declare an anonymous class withing the context of a method, it could not have static methods.

like image 187
Brett Okken Avatar answered Oct 29 '22 14:10

Brett Okken


It may have to do with the fact that Enums are compiled similarly to classes. For the same reason you cannot declare a class definition inside a method, you cannot declare in enum either.

like image 29
Adam Yost Avatar answered Oct 29 '22 12:10

Adam Yost