In The Java Programming Language by James Gosling its specified that
"As with other anonymous inner classes, the enum constant class body can define arbitrary instance fields, and methods, but it can't declare static members or define constructors. Also note that because enum constants are implicitly static fields, these anonymous inner classes have no enclosing instance."
i tried to do that in following code and get error
"The field pieceType cannot be declared static; static fields can only be declared in static or top level types" (what does it mean)
package com.example;
enum ChessPiece{
PAWN{
@Override
void pieceName(String name) {
// TODO Auto-generated method stub
System.out.println("PAWN");
}
},
ROOK{
@Override
void pieceName(String name) {
// TODO Auto-generated method stub
System.out.println("ROOK");
}
},
QUEEN{
static String pieceType = "QUEEN"; // ERROR
@Override
void pieceName(String name) {
// TODO Auto-generated method stub
System.out.println("QUEEN");
}
};
abstract void pieceName(String name);
}
why is it so ?
QUEEN
class it makes no difference, just declare the field as non-static instead. – Ian Roberts