Home > JAVA > Enum

Enum

  1. An enum specifies a list of constant value asigned to a type.
  2. An enum is not a String or int, an enum constant type is the enum type.
  3. An enum can be declared outside or inside a class, but not in a method.
  4. An enum declared outside a class mus not be marked static, final, abstract, protected or private.
  5. Enums can contain constructors, methods, variables and constant class bodies.
  6. enum constants can send arguments to the enum constructor.
  7. enum constructors can have arguments and can be overloaded.
  8. enum constructors can never been invoked directly in code. They are always called automatically when an enum is initialized.
  9. The semicolon at the end of an enum is optional.
  10. MyEnum.value() return an array of MyEnum’s values.

declaring enum outside a class

enum CollorType {BLUE, RED, YELLOW}

class Collor {
CollorType collorType;
}

public class CollorTest1 {
public static void main(String[] args){
Collor collor = new Collor();
collor.collorType = CollorType.BLUE
}
}

declaring enum inside a class

class Collor {
enum CollorType {BLUE, RED, YELLOW}
CollorType collorType;
}

public class CollorTest1 {
public static void main(String[] args){
Collor collor = new Collor();
collor.collorType = CollorType.BLUE
}
}

 

Enum with constructor

enum CollorType {
BLUE(10), RED(15), YELLOW(20);
CollorType(int saturation) {
this.saturation = saturation;
}
private int saturation;
public int getSaturation() {
return saturation;
}
}

public class CollorTest1{
CollorType collorType;
public static void main(String[] args) {
CollorTest1 collorTest1 = new CollorTest1();
collorTest1.collorType = CollorType.BLUE;

System.out.println(collorTest1.collorType.getSaturation); //print 10
}
}
Categories: JAVA
  1. No comments yet.
  1. July 1, 2014 at 2:00 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: