Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Enum - Constant Specific Body

 
Ranch Hand
Posts: 62
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

Enum is again troubling me

I have seen that we can overload a method in enums. Also, I have seen that we can define a method in an enum and overload the method (change the args) in constant specific body. This compiles fine. But when I try to call the method defined in consant specific body the compiler complains. Why is this so?

The following is my code. The line in bold creates the issue

public enum Seasons {

SUMMER(10),
WINTER {
public int getSeasonValue() {
return 100;
}
},
AUTUMN {
public int abc = 1999;
public int getSeasonValue(int x) {
return 20;
}
},
SPRING;

private int seasonValue;

Seasons(int x) {
seasonValue = x;
}

Seasons() {
}

public int getSeasonValue() {
return seasonValue;
}
public static void main(String args[]) {
Season season = new Season();
System.out.println("SUMMER: "+season.seasonS.getSeasonValue());
System.out.println("WINTER: "+season.seasonW.getSeasonValue());
System.out.println("AUTUMN: "+season.seasonA.getSeasonValue(10));
}
}

class Season {
Seasons seasonS = Seasons.SUMMER;
Seasons seasonW = Seasons.WINTER;
Seasons seasonA = Seasons.AUTUMN;
}
 
Sheriff
Posts: 9690
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well the answer is simple, the methods that you define in enum specific body are like methods in anonymous inner classes. So you can only call the methods available in the enum just like you can only call the methods available in the super class of an anonymous inner class

 
Jisha Anand
Ranch Hand
Posts: 62
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it; thanks a lot Ankit..

Even if the method is declared public there is no way to access the method outside the class..same with enum right?
 
Marshal
Posts: 22461
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right.
reply
    Bookmark Topic Watch Topic
  • New Topic