Why is Enum allowed to have a main method? Does it work as a normal main method? What is the purpose of having a main in Enum? Is main taken like a normal method?
Asked
Active
Viewed 67 times
-1
-
1In Java `enum` is a special form of `class`, so it may contain a `main()` method... – Usagi Miyamoto Oct 15 '18 at 08:45
-
Similar to this https://stackoverflow.com/questions/512877/why-cant-i-define-a-static-method-in-a-java-interface – jschnasse Oct 15 '18 at 08:46
-
If you’re asking for a reason, it’s probably either too opinion-based or just guess-work. Neither is well suited for Stack Overflow. – Ole V.V. Oct 15 '18 at 09:18
2 Answers
2
Why legislate against it ? Because it seems "common sense" to some "majority" who look at things from an inevitably very specific perspective ? In language design it is generally a bad idea to try and legislate "common sense" upon its users.

Erwin Smout
- 18,113
- 4
- 33
- 52
-
I asked a reason, I've studied C. A reason was all. Thank you for the enlightenment. – Shreyash Sharma Oct 15 '18 at 09:08
1
An enum
is nothing more than a final
class with a private
constructor and a given set of instances. There's nothing special about it with regards to other methods it can contain, and a main method is fine.

daniu
- 14,137
- 4
- 32
- 53