aspose file tools*
The moose likes Beginning Java and the fly likes enums Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "enums" Watch "enums" New topic
Author

enums

Rajiv Chelsea
Ranch Hand

Joined: Jun 15, 2010
Posts: 88


Why enum declaration is not allowed inside methods?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Think of them as a type of class. Can you declare a class in a method?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
vikas mhatre
Greenhorn

Joined: Apr 24, 2010
Posts: 26
Can you declare a class in a method?


As a method local inner class
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Bear Bibeault wrote:Think of them as a type of class. Can you declare a class in a method?

Yep.
Rajiv Chelsea
Ranch Hand

Joined: Jun 15, 2010
Posts: 88


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

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Because it's not a valid identifier.
Rajiv Chelsea
Ranch Hand

Joined: Jun 15, 2010
Posts: 88
As read, enums are used for contants , so
can't we use primitive constants in it?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Because the enum names must follow the rules for Java identifiers. And you can't declare a variable called 3, now can you?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rajiv Chelsea
Ranch Hand

Joined: Jun 15, 2010
Posts: 88
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

Joined: Mar 20, 2007
Posts: 317
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

Joined: Jun 15, 2010
Posts: 88




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

Joined: Oct 27, 2005
Posts: 19723
    
  20

Because in the second example, the enum is not CoffeeSize - it's Coffee.CoffeeSize. It's a nested enum.
Rajiv Chelsea
Ranch Hand

Joined: Jun 15, 2010
Posts: 88
Thanks for all the answers..
Rajiv Chelsea
Ranch Hand

Joined: Jun 15, 2010
Posts: 88
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?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: enums