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 {};
}
I’ve gone ahead and built a little project containing the source you posted and just enough framework around it to make it compile. I got 3 class files: Circuit.class, CircuitType.class and Auditor.class – as expected.
All this under Java 1.6.
But as others have indicated, I think your diagnosis of the problem is off.Anonymous classes are easy to generate accidentally: Typically a construct like
will create one, for example. Given more of your code, the good SO folks might be able to pinpoint your error.It might be interesting and instructive to disassemble your class files with
javapor better yet a “real” Java disassembler likeJD.Update
Added your new Auditor code to mine… no change. No anonymous classes.
Your code is of course correct (to the extent we can see it) but the design is not very OO. Some people would point out that you’ll have to extend your counter declarations and your
switchstatement every time a new circuit type appears.You’re also not making much use of the “special features” of enums. I have a much simplified version of your
Auditormethod:Update 2
I found your
javapoutput quite illuminating. See my comment below.My conclusions:
switchmakeand useantto embrace the anon classes and other strangenesses of Java.Since you’re only having problems because of your non-standard compilation setup, I’d go with the last solution and attack the problem there.