This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declaring Enums in a class

 
Tahir Akram
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.???
 
Mark Moge
Ranch Hand
Posts: 100
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
phil sohan wrote:
Whether the enum is implicitly static.???

if an enum is a member of a class, it is implicitly static
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's not so that when enums are declared in a class they are static ...

they are static no matter where they are declared ...
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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 !!!

 
Sahil Kapoor
Ranch Hand
Posts: 316
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 316
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Abimaran

Thanks !!!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sahil Kapoor wrote:@Abimaran

Thanks !!!


Welcome!
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic