• 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

enums

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it safe to say that all method inside an enum declaration is to be considered static?
 
Saloon Keeper
Posts: 15608
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.

How did you arrive at this conclusion?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Method or variable can not be static unless you explicitly add the "static" keyword in their declaration.

So, enum method can NOT be implicitly be treated as static...but enum COULD contain static methods
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its worth to note that compiler adds two static method with every enum values() and valueOf(), may be due to this reason you thought all method would be static but as said here not necessary.
 
Bert Bumper
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Javin Paul wrote:Its worth to note that compiler adds two static method with every enum values() and valueOf(), may be due to this reason you thought all method would be static but as said here not necessary.



it got me confuse with the way enums are instantiated (pardon the term if its wrong)



i was confuse by line 30 to thinking that Building reference is where the getHeight() method was called where in fact it is from the
Building.HIGH_RISE instance thanks for the reply
 
Prabhjot Jassal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the variable "building" is of static type but the method getHeight() is not. It is important to note that there could exist only one 1 enum in memory eg

Building b1 = Building.LOW_RISE;
Building b2 = Building.LOW_RISE;

boolean areEqual = b1 == b2;

the value of areEqual is "true"

Could someone please clarify this as well...i am not 100% sure on this
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct. There is only one instance of each enum element.
 
reply
    Bookmark Topic Watch Topic
  • New Topic