Skip to content

Instantly share code, notes, and snippets.

@karanmalhi
Created June 13, 2011 19:00
Show Gist options
  • Save karanmalhi/1023444 to your computer and use it in GitHub Desktop.
Save karanmalhi/1023444 to your computer and use it in GitHub Desktop.
Explains Enums in java
package learnquest;
public class Rectangle {
private final Size size;
public Rectangle(Size size) {
this.size = size;
}
public Size getSize() {
return size;
}
public void getArea() {
size.area();
}
public static void main(String[] args) {
Rectangle small = new Rectangle(Size.SMALL);
Rectangle med = new Rectangle(Size.MEDIUM);
Rectangle large = new Rectangle(Size.LARGE);
small.getArea();
med.getArea();
large.getArea();
}
private static void foo(Size size){
switch(size){
case SMALL:
;
case MEDIUM:
;
case LARGE:
;
}
}
}
enum Size {
SMALL(5, 5) {
public void area() {
System.out.println("Area of SMALL");
}
},
MEDIUM(7, 7) {
@Override
public void area() {
System.out.println("Area of MEDIUM");
}
},
LARGE(10, 10) {
@Override
public void area() {
System.out.println("Area of LARGE");
}
};
private final int height;
private final int width;
private Size(int height, int width) {
this.height = height;
this.width = width;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public abstract void area();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment