views:

222

answers:

3

I'm getting an anonymous class at compile-time that I'm not expecting. Relevant code follows, then a more detailed explanation:

Entirety of CircuitType.java:

public enum CircuitType { V110A20, V110A30, V208A20, V208A30 }

From Auditor.java, lines 3-9:

public class Auditor {
    private String[] fileNames;
    private int numV110A20;
    private int numV110A30;
    private int numV208A20;
    private int numV208A30;

From Auditor.java, lines 104-121:

[...]
switch (newCircuit.getType()) {
    case V110A20:
        this.numV110A20++;
        break;
    case V110A30:
        this.numV110A30++;
        break;
    case V208A20:
        this.numV208A20++;
        break;
    case V208A30:
        this.numV208A30++;
        break;
    default:
        System.err.println("An Error Has Occured.");
        System.exit(-1);
        break;
}
[...]

From Circuit.java, lines 1-5:

public class Circuit {
    private CircuitType myType;
    public CircuitType getType() {
        return this.myType;
    }
[...]

When the command

javac *.java

is executed, an anonymous class Auditor$1.java is generated. The files, obviously, all sit next to each other in a file system directory that contains nothing else.

When lines 104-121 are commented out, no anonymous class is generated.

I at first thought it was a package issue, so put the three classes in a package, but I didn't know enough about packages to get it working. If it's truely a package issue, can someone step me through exactly how to label them? I'd rather not have to package them if I don't have to, though.

The reason the anonymous class is a problem, besides the fact that such classes usually signify a namespace issue, is that it breaks my Makefile I use for automatic compilation.

Update


Attached is a console session which I hope may shed light on this mystery:

% javap 'Auditor$1'
Compiled from "Auditor.java"
class Auditor$1 extends java.lang.Object{
    static final int[] $SwitchMap$CircuitType;
    static {};
}
+1  A: 

I seem to have found the solution right before I posted the question, but, in the spirit of shared knowledge, posted it anyway. It might be related to this question, which in turn is related to this question.

Maarx
Jury is still out on whether or not this, in fact, has anything to do with my original problem.
Maarx
Turns out it kind-of-does-but-really-doesn't.
Maarx
+2  A: 
Carl Smotricz
I included more code from Auditor.java, per your request. The line 104-121 code block is for-sure the culprit, as the anonymous class is not generated when that code block is commented out. I believe I've included everything relevant to that code block. If you need anything else, please let me know.
Maarx
Updated my answer to respond to your updates.
Carl Smotricz
My mind is blown. Twice. There is definitely an anonymous class for me with the switch statement, and there is definitely no anonymous class when the switch statement is commented out. When you said you still didn't get an anonymous class, I moved the code from my UNIX development environment to a throw-away Windows virtual machine with the latest JDK update and recompiled. I still get an anonymous class with the switch statement, and none without.
Maarx
Your method of tallying was also a surprise. I am rewriting my switch with your method, both because it's better and to see if maybe it resolves the anonymous class issue.
Maarx
@Carl: I have updated my question with the output of 'javap'. While I've never used 'javap' before and didn't even know about it until you mentioned it, the output seems rather definitively involved with the nature of the switch statement. However, I cannot divine anything further.Perhaps you can?
Maarx
Yes... the javap is indeed interesting. Apparently your particular Java implementation uses this anonymous class to implement the switch-on-enum feature. Thing to realize is, the java `switch` statement really only 'does' `int`s. Most of these cool new language features are just syntactic sugar on "basic" Java. Seems the switch statement is implemented by looking up the enum in a map and then switching on the int stored as the value.
Carl Smotricz
Out of curiosity, what java implementation are you using that DOESN'T make an anonymous class for the switch? I'd think installing the most recent JDK right off Sun's website onto a pre-built HP Compaq Core2 vPro running Windows Vista is about as standard an implementation as you could get, and I still got an anonymous class.
Maarx
I'm doing most of my Java work using the Eclipse IDE. Some other people have mentioned that this problem doesn't happen with Eclipse. As far as I know there's an IBM compiler built into Eclipse to do the incremental compilation thing. It kinda sidesteps the installed compiler, I think. The compiler is very good; in this particular case, apparently better than the standard :)
Carl Smotricz
+3  A: 

It indeed appears that (in certain cases at least) an inner class will be generated for the switch statement:

http://stackoverflow.com/questions/1834632/java-enum-and-additional-class-files

ZoogieZork
Very intersting, +1!
Carl Smotricz