2
2
.
.
4
4
.
.
4
4
E
E
n
n
u
u
m
m
e
e
r
r
a
a
t
t
o
o
r
r
s
s
-
-
T
T
e
e
m
m
p
p
l
l
a
a
t
t
e
e
I
I
n
n
f
f
o
o
Template Enum is Data Type which contains Constant Templates which are made from
Constant Strings (which serve as Template's Names)
list of Constants inside brackets (number of Constants and their Data Types can be different between Templates)
Template Enum must have
Properties for storing Template's Constant when specific Template is assigned to a Variable
(if maximum number of Constants in a Template is 3 then there should be at least 3 Properties)
Constructor for each number of Template Constants
(if there is a Template that has 3 Constants then there should be a Constructor that accepts 3 Parameters)
Methods that can freely operate on these Properties
(not necessarily just returning Properties, they can have Input Parameters)
Template Enum can be used
by declaring Variable of specific Enum Data Type
and then storing any of the Enum's Template Names inside it (you don't specify parameters here)
Constants from a Template's list are then used to call Constructor that has that number of Parameters
Constructor sets the Enum Properties
if some Properties are not explicitly set by the Constructor, they will be set to default values (depending on Data Type)
S
S
y
y
n
n
t
t
a
a
x
x
Template Enum is declared by
using Keyword enum enum
followed by required Enum 's Name Day
followed by required Enum 's Body { ... }
Template Enum Syntax
//===========================================================================================================
// ENUM: Day
//===========================================================================================================
enum Day {
//CONSTANT TEMPLATES WITH VARIABLE PARAMETERS AND DATA TYPES.
SUNDAY,
MONDAY ("John"),
TUESDAY ("John", 20),
WEDNESDAY,
THURSDAY,
FRIDAY (40, 50, 60),
SATURDAY;
//PROPERTIES FOR STORING ABOVE PARAMETERS THROUGH CONSTRUCTORS. PLUS ANY ADITIONAL PROPERTIES IF NEEDED.
private String name;
private int age;
private int middle;
private int end;
//CONSTRUCTOR WITH 0 PARAMETERS.
private Day() {
this.middle = 100;
}
//CONSTRUCTOR WITH 1 PARAMETER.
private Day(String name) {
this.name = name;
}
//CONSTRUCTOR WITH 2 PARAMETERS.
private Day(String name, int age) {
this.name = name;
this.age = age;
}
//CONSTRUCTOR WITH 3 PARAMETERS.
private Day(int age, int middle, int end) {
this.age = age;
this.end = end;
}
//CUSTOM METHODS THAT OPERATE ON PROPERTIES.
public String getName () { return this.name; }
public int getAge () { return this.age; }
public int getEnd () { return this.end; }
public int setAge (int age) { return this.age = age; }
}
//===========================================================================================================
// CLASS: Test
//===========================================================================================================
public class Test {
public static void main(String[] args) {
//CREATE ENUM VARIABLE WITH PREDEFINED VALUE.
Day sunday = Day.SUNDAY;
Day monday = Day.MONDAY;
Day tuesday = Day.TUESDAY;
Day friday = Day.FRIDAY;
//DISPLAY PARAMETERS.
System.out.printf("%s %n" , sunday);
System.out.printf("%s: Hello %s %n" , monday ,monday .getName());
System.out.printf("%s: %s is %d years old %n", tuesday,tuesday.getName(),tuesday.getAge());
System.out.printf("%s: (%s,%d,%d) %n" , friday ,friday .getName(),friday
.getAge(),friday.getEnd());
//CHANGE PROPERTY.
friday.setAge(1000);
System.out.println(friday + " Age = " + friday.getAge()); //1000
}
}