tbodt tbodt - 1 month ago 12
Java Question

Very odd compilation error with varargs

I am writing an enum for all the opcodes in the JVM. It isn't complete, and looks like this so far:

public enum Opcode {
NOP(),
ACONST_NULL(),
ICONST_M1(),
ICONST_0(),
ICONST_1(),
// a zillion more of these
JSR_W();

private Opcode(Class<? extends Argument> args...) {
}
}


There is a compilation error on the line of the construction declaration:


')' expected


What is going on?

Answer

The ... notation goes on the parameter type not on the parameter name, like so

private Opcode(Class<? extends Argument>... args) {
}

For thoroughness, the Java Language Specification states that a method's parameter list has the following form

FormalParameterList:
    LastFormalParameter
    FormalParameters , LastFormalParameter

where LastFormatParameter has the form

LastFormalParameter:
    VariableModifiersopt Type... VariableDeclaratorId
    FormalParameter

The ... comes after the parameter type declaration.

Comments