This is code from a question on an SCJP practice test. The answer output is Zippo, but I don't understand why it compiles. I have notes that say a method-local inner class can only be instantiated in that method. If an attempt is made to use it outside the method, it won't compile. Did I misunderstand the book I took notes from or am I missing something with this code? Thanks.
Oh, you're right, it is. For some reason I was thinking it was outside of main. I was misreading the code, now I don't know how I missed it. Thanks for the help.
Yes, you can define classes inside methods like that. It's a feature of Java that isn't used very much, and I guess many Java programmers don't even know that it is possible. The normal rules for scoping apply: class Horse will not be visible outside the { ... } of the method in which it is defined.