• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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.
 
You can't expect to wield supreme executive power just because
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic