aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Declaring Enums in a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Declaring Enums in a class" Watch "Declaring Enums in a class" New topic
Author

Declaring Enums in a class

Tahir Akram
Ranch Hand

Joined: Jul 03, 2007
Posts: 37
Hi rachers;

I want to understand the concept of calling BallSize enum via line 1 and line 2.

What is the difference between them. As they compiles fine.



Tahir Akram
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Tahir Akram wrote:Hi rachers;

I want to understand the concept of calling BallSize enum via line 1 and line 2.

What is the difference between them. As they compiles fine.


Try to access from different class. One will work, Other won't work.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

yes.....enum is global to a class so can acess it without a refernce of the class.

and when you want to access it from outside the class you have to use the refernce of the class.


so both will work fine...................

One of the most importanct question,that come into my mind rightnow is that...........
As we reference enum with a static(main) method then why not it is throughing Exception like"Non-static cannot be acessed from a static context"

Whether the enum is implicitly static.???


SCJP6.0,My blog Ranchers from Delhi
Mark Moge
Ranch Hand

Joined: Mar 14, 2009
Posts: 96

phil sohan wrote:
Whether the enum is implicitly static.???

if an enum is a member of a class, it is implicitly static


SCJP6 | http://programmerutilities.blogspot.com/
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

phil sohan wrote:
One of the most importanct question,that come into my mind rightnow is that...........
As we reference enum with a static(main) method then why not it is throughing Exception like"Non-static cannot be acessed from a static context"

Whether the enum is implicitly static.???


It's not Exception, I think, you supposed to say Compilation Error!

There are no inner, local, or anonymous interfaces or enums. And if you declare the enum as inner, it implicitly static(top level) enums.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

hi,,,,,,,,,,yes.......i nearly got there after postin my earlier post.......
enum can be accessed within a class directly........and reason why it is getting accessed inside the static method directly and not throughing exception like "Non-static cannot be accessed from a static context"is that......
Remember few things about enum
1.)enums are implicitly final subclasses of java.lang.Enum
2.)if an enum is a member of a class, it is implicitly static
3.)new can never be used with an enum, even within the enum type itself
4.)name and valueOf simply use the text of the enum constants, while toString may be overridden to provide any content, if desired
5.)for enum constants, equals and == amount to the same thing, and can be used interchangeably
enum constants are implicitly public static final
the order of appearance of enum constants is called their "natural order", and defines the order used by other items as well : compareTo, iteration order of values , EnumSet, EnumSet.range.


may be you are clear by seeeing this points......

Tahir Akram
Ranch Hand

Joined: Jul 03, 2007
Posts: 37
Thanks for making my concept good about Enums.

- They are static if they are declared in a class. -- its a new thing for me.
- And this thing really make sense. That in Ball class, BallSize enum can be accessed without class name. But when we made another class, we need a class name (Ball) to access it.

Thanks again.
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 383

it's not so that when enums are declared in a class they are static ...

they are static no matter where they are declared ...


http://plainoldjavaobject.blogspot.in
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
@ lalit

it's not so that when enums are declared in a class they are static ...

they are static no matter where they are declared ...


I think you are referring constants inside enum declaration. Like enum Names {SAHIL, KAPOOR;};

But , above your post they are talking of Names and not SAHIL , KAPOOR.

So NAMES is implicitly static if it is declared inside class.
At class level....Nothing can be static , in fact id does not make sense for interface, enum or class to be static at class level (Top-level).

Thanks !!!



SCJP 6.0 96%

(Connecting the Dots ....)
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
@ Abimaran......
You are right that enum (Names) is implicitly static when declared inside class....But i think saying it top-level is wrong notion. If something is defined inside the scope of another, it is no more top level. Infact in K&B , they also says that Nested classes are top-level, dont you think its a wrong notion ???

What do you say ??

Thanks !!!
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Lalit Mehra wrote:it's not so that when enums are declared in a class they are static ...

they are static no matter where they are declared ...


If the enum is declared in side a class(nested enum), it(the enum class) is implicitly static. It's like a top level class. If the enum isn't declared in any method, it's just a enum class(top level) which extends java.lang.Enum.


Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Sahil Kapoor wrote:@ Abimaran......
You are right that enum (Names) is implicitly static when declared inside class....But i think saying it top-level is wrong notion. If something is defined inside the scope of another, it is no more top level. Infact in K&B , they also says that Nested classes are top-level, dont you think its a wrong notion ???

What do you say ??

Thanks !!!


Not all nested class are top level class, but nested enums, interface, static inner class are only can behave like top level class, means no need of any(Outer class) instances to create it's own instance! May that be the reason for calling them as like top level classes.
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
@Abimaran

Thanks !!!
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Sahil Kapoor wrote:@Abimaran

Thanks !!!


Welcome!
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 383

well yes i obviously meant the enum members ...

because an enum cannot be marked static or final outside the class
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Declaring Enums in a class
 
Similar Threads
Issue with SliderBar
Enthuware question about polymorphism
BrickBreaker code error
about enum
what is need of super() in the foolowing line of code?