Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

enum in method declaration  RSS feed

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Why enum cannot be declared with in a method ?
 
Author and ninkuma
Marshal
Posts: 66727
167
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enums are in the same category as classes. You can't declare classes in methods either, can you?
 
author
Sheriff
Posts: 23553
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be more exact, for classes, you are not allowed to declare static members for local classes. For enums, the members of the enum are static, so it is not possible to define a usable enum type at a method level.... hence, it is just not allowed.

Henry
 
Raja Narasimha
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. As per the following program we declare a class in a method. Please let me know if you are referring this or something else.

Bear Bibeault wrote:Enums are in the same category as classes. You can't declare classes in methods either, can you?

 
Raja Narasimha
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I got your point.

Henry Wong wrote:To be more exact, for classes, you are not allowed to declare static members for local classes. For enums, the members of the enum are static, so it is not possible to define a usable enum type at a method level.... hence, it is just not allowed.

Henry

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66727
167
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Henry's better explanation.
 
Henry Wong
author
Sheriff
Posts: 23553
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Raja Narasimha wrote:Thank you. I got your point.

Henry Wong wrote:To be more exact, for classes, you are not allowed to declare static members for local classes. For enums, the members of the enum are static, so it is not possible to define a usable enum type at a method level.... hence, it is just not allowed.



Of course, this is speculation. The correct answer is... it is not allowed because the JLS says that it is not allowed.

Henry
 
Henry Wong
author
Sheriff
Posts: 23553
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raja Narasimha,

Your post was moved to a new topic.
 
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!