• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Declaring Enums in a class

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.???
 
Ranch Hand
Posts: 101
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Abimaran

Thanks !!!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sahil Kapoor wrote:@Abimaran

Thanks !!!



Welcome!
 
Lalit Mehra
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic