6

I'm learning Enum and got confused about this code.

enum Currency{
    PENNY, NICKLE, DIME, QUARTER;
    @Override
    public String toString() {
            switch (this) {
                case PENNY:
                    System.out.println("Penny: ");
                    break;
                case NICKLE:
                    System.out.println("Nickle: ");
                    break;
                case DIME:
                    System.out.println("Dime: ");
                    break;
                case QUARTER:
                    System.out.println("Quarter: ");
            }
        return super.toString();
    }
};
public class Check{
    public static void main(String[] args){

    }
}

When I compiled the javac Check.java I'm getting the following .class files.

Check.class
Currency$1.class
Currency.class

Why Currency$1.class is created? and how? what is the reason behind? I know $ sign for inner classes and 1 mean anonymous class 1. But why in this code it created as there is no inner class.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • 4
    @MageXy I'm not asking about what is the $ sign classes. I'm asking why it created here? –  Apr 25 '16 at 15:16
  • Take a look at the question I linked. The accepted answer explains why you're getting those class files. – Mage Xy Apr 25 '16 at 15:17
  • The answer is contained in that question. The current enum has an implicit inner class: http://stackoverflow.com/a/11388863/5969411 – ManoDestra Apr 25 '16 at 15:18
  • You should really override `toString` in each enum value separately. – flakes Apr 25 '16 at 15:18
  • There's no harm in providing a "bespoke" answer, IMHO. – Bathsheba Apr 25 '16 at 15:20
  • 3
    Where's the inner class inside the enum? I also dont understand why it's generating an extra class file.. And the linked question only explains about anonymous inner classes, nothing about an implicit class inside enums... – Codebender Apr 25 '16 at 15:27
  • 4
    It's a synthetic helper class for the switch. http://stackoverflow.com/a/4319152/2891664 – Radiodef Apr 25 '16 at 15:31
  • switch (this) is the issue. Inside the specific enum, it is entirely unnecessary to do a switch statement. It is forcing the inspection to look outside of the enum. It would be adequate to simply `System.out.println(this.name());` instead. Unless a subset is required, in which case you're going to be referring from outside the enum and an additional implicit class may be compiled for this purpose. Oh, and BTW, it's `Nickel`, not `Nickle` :) – ManoDestra Apr 25 '16 at 15:31
  • @ManoDestra instead of switch or inside each Object's implementation. `PENNY { /***/ };` any other situation where it could happen? –  Apr 25 '16 at 15:38
  • @LetDoit Essentially, anywhere that creates an implicit inner class. It's hard to say all the scenarios where that could occur. In your example above, you're already inside the enum and it will only have one value, so there's not a great use case for having to check the values with a switch or if, I would have thought. But, if you do, then the $1 situation could occur. I'd keep my enum code pretty simple and do any selective logic outside of the enum itself, personally. – ManoDestra Apr 25 '16 at 15:45

0 Answers0