Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This is to demonstrate how enums are just syntactic sugar. But this is not complete. What about hashValue, equals, compareTo, name, toString?
- */
- ---------------------------------------------
- package com.example;
- import java.awt.Color;
- public final class MyEnum {
- public static final MyEnum ENUM_APPLE = new MyEnum(Color.GREEN);
- public static final MyEnum ENUM_BANANA = new MyEnum(Color.YELLOW);
- private final Color color;
- private MyEnum(final Color _color) {
- this.color = _color;
- }
- public Color getColor() {
- // OK, because Color is immutable. Otherwise return a clone:
- return color;
- }
- }
- ---------------------------------------------
- public enum MyEnum {
- ENUM_APPLE (Color.GREEN), ENUM_BANANA (Color.YELLOW);
- private final Color color;
- private MyEnum(final Color _color) {
- this.color = _color;
- }
- public Color getColor() {
- // OK, because Color is immutable. Otherwise return a clone:
- return color;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement