Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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 ?
 
Marshal
Posts: 67197
169
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
Posts: 23811
140
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
Marshal
Posts: 67197
169
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
Posts: 23811
140
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
Posts: 23811
140
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.
 
My, my, aren't you a big fella. Here, have a tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!