Thursday, February 3, 2011

Java Enums Fun

As I am studying for the SCJP certification for Java I am going to mention some things in the next few months regarding Java Programming.

Currently Reading about ENUMS

So you can create an enum with a constructor.

  • This is implicitly private.
  • Java will only compile with default and private access modifier.


public enum Cup {

SMALL(4), LARGE(8), HUGE(16);
private final int size;

private Cup(int size) {
this.size = size;
}

public int size() {
return size;
}
}

public enum Plate {

SMALL(4), LARGE(8), HUGE(16);
private final int size;

Plate(int size) {
this.size = size;
}

int size() {
return size;
}
}



Notice the private Cup(int size) constructor and the Plate(int size) constructor. Both of these mean private constructor.

No comments:

Post a Comment