• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

enums

 
Rajiv Chelsea
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why enum declaration is not allowed inside methods?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64832
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of them as a type of class. Can you declare a class in a method?
 
vikas mhatre
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you declare a class in a method?


As a method local inner class
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Think of them as a type of class. Can you declare a class in a method?

Yep.
 
Rajiv Chelsea
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why does the above line of code give syntax error on token "3"?

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it's not a valid identifier.
 
Rajiv Chelsea
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As read, enums are used for contants , so
can't we use primitive constants in it?
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the enum names must follow the rules for Java identifiers. And you can't declare a variable called 3, now can you?
 
Rajiv Chelsea
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An enum type is a type whose fields consist of a fixed set of constants. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
Because they are constants, the names of an enum type's fields are in uppercase letters.


The above is the definition of enum as given in Sun Doc,
basically its used for constants..

So why have been primitive constants disaalowed?

The "3" i gave in my enum declaration is not an identifier, its a constant which
is what enums are meant for..
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BIG,HUGE,OVERWHELMING are all identifiers for a constant. 3 however is a constant and cannot be used as an identifier.

Something similar to this...
 
Rajiv Chelsea
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




I have the above two classes.
In the second code block, an error occurs saying class Coffee already exists in the package.
Why does not the same thing occur for enum CoffeeSize as i have declared it in both the
blocks of code and enum is also a type of class?
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because in the second example, the enum is not CoffeeSize - it's Coffee.CoffeeSize. It's a nested enum.
 
Rajiv Chelsea
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the answers..
 
Rajiv Chelsea
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the Java language designers made it optional to
put a semicolon at the end of the enum declaration (when no other declarations for
this enum follow):


Can I have some example codes of where semicolon is optional
and where semicolon is required?

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic