Is there a way to implement algebraic types in Java?
Give class A
a constructor with package-level accessibility (and no other constructors).
Thanks, Dave L., for the bit about no other constructors.
You probably want an enum (Java >= 1.5). An enum type can have a set of fixed values. And it has all the goodies of a class: they can have fields and properties, and can make them implement an interface. An enum cannot be extended.
Example:
enum A {
B,
C,
D;
public int someField;
public void someMethod() {
}
}