• 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 with switch statement

 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Till now I was having an understanding that when we define a class inside a class after compilation we get one outer.class and another outer$inner.class

I have got an example of enums with switch case

enum Apple{
Jonathan,GoldenDel,RedDel,Winesap,Cortland
}

class EnumDemo {
public static void main(String[] args) {
Apple ap;
ap=Apple.RedDel;
System.out.println("Value of ap:"+ap);
//System.out.println(Apple.RedDel);
System.out.println();

//Compare two enum values
ap=Apple.GoldenDel;
if(ap==Apple.GoldenDel)
System.out.println("ap contains GoldenDel.\n");

//Use an enum to control a switch statement
switch(ap){
case Jonathan:
System.out.println("Jonathan is red.");
break;
case GoldenDel:
System.out.println("Golden Delicious is Yellow.");
break;
case RedDel:
System.out.println("Red Delicious is red");
break;
case Winesap:
System.out.println("Winesap is red.");
break;
case Cortland:
System.out.println("Cortland is red.");
break;
}
}
}


Here after compilation I am getting two files generated EnumDemo.class and EnumDemo$1.class why EnumDemo$1.class although I donot have any inner class.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note: Please use code tags when you post code, that makes your code much easier to read.

I have compiled your code (with JDK 1.6.0_02 on Windows XP) and got the following class files:

Apple.class
EnumDemo.class
EnumDemo$1.class

To find out what EnumDemo$1.class contains, I disassembled it with javap (a tool that is included with the JDK). This is what I got:

Note the line with "$SwitchMap$Apple". It looks like the compiler generates a class that behind the scenes is needed to implement the switch statement efficiently. So, classes with names like "...$1" are not necessarily anonymous inner classes; the compiler also does this for other classes that it generates itself behind the scenes.

Note that while this is an interesting topic, this is not something you need to know for the SCJP exam.
[ August 15, 2007: Message edited by: Jesper Young ]
 
Gaurav Gambhir
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper,
Sure in future I will make use of code tags.
 
What? What, what, what? What what tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic