R
rfractal30
Hi
I was looking at 'enumerated types'in a tutorial and I came across some
strange recursive situations with I couldn't understand.
I can see the point of enumerated types as it limits the ways in which
an object can be instanciated. Take the code below. It would not be
possible to create an object like this:
monthes mymonth = new monthes("fred");
Obviously that does not work, because the constructor is private. Only
if I were creating an instance of monthes from within that class can I
do that. So I have enumerated types to create instances of monthes, for
example:
monthes mymonth = monthes.AUGUST;
This is fine, but it occured to me that this could lead to a weird kind
of recursion. I tested it and my suspicions turned out to be correct.
It was therefor possible to create code such as this:
System.out.println(mymonth.JANUARY.FEBUARY.MARCH.APRIL.NOVEMER.SEPTEMBER.JANUARY.JULY.DECEMBER.MAY.FEBUARY.getMonth());
Crazy!!! no? I really can't understand how this is possible (but I
tested it and it really is) How does this work? Why does the program
not crash due to infinite recursion. I'm confused.
//____________________________________________________
public class monthes {
static final monthes JANUARY = new monthes ("January");
static final monthes FEBUARY = new monthes ("Febuary");
static final monthes MARCH = new monthes ("March");
static final monthes APRIL = new monthes ("April");
static final monthes MAY = new monthes ("May");
static final monthes JUNE = new monthes ("June");
static final monthes JULY = new monthes ("July");
static final monthes AUGUST = new monthes ("August");
static final monthes SEPTEMBER = new monthes ("September");
static final monthes OCTOBER = new monthes ("October");
static final monthes NOVEMBER = new monthes ("November");
static final monthes DECEMBER = new monthes ("December");
private String month;
private int numDays;
private monthes(String inmonth) {
if (inmonth.equals("January") || inmonth.equals("March") ||
inmonth.equals("May") || inmonth.equals("July") ||
inmonth.equals("August") || inmonth.equals("October") ||
inmonth.equals("December")) {
numDays = 31;
}
if (inmonth.equals("April") || inmonth.equals("June") ||
inmonth.equals("September") || inmonth.equals("November")) {
numDays = 30;
}
if (inmonth.equals("Febuary")){
numDays = 28;
}
month = inmonth;
}
public String getMonth() {
return(month);
}
public int getNumDays() {
return(numDays);
}
}
}
public class TestMonthes {
public static void main(String[] args) {
monthes mymonth = monthes.AUGUST;
System.out.println(mymonth.JANUARY.FEBUARY.MARCH.APRIL.NOVEMER.SEPTEMBER.JANUARY.JULY.DECEMBER.MAY.FEBUARY.getMonth());
}
}
//____________________________________________________
Thanks for any advice.
Michael
I was looking at 'enumerated types'in a tutorial and I came across some
strange recursive situations with I couldn't understand.
I can see the point of enumerated types as it limits the ways in which
an object can be instanciated. Take the code below. It would not be
possible to create an object like this:
monthes mymonth = new monthes("fred");
Obviously that does not work, because the constructor is private. Only
if I were creating an instance of monthes from within that class can I
do that. So I have enumerated types to create instances of monthes, for
example:
monthes mymonth = monthes.AUGUST;
This is fine, but it occured to me that this could lead to a weird kind
of recursion. I tested it and my suspicions turned out to be correct.
It was therefor possible to create code such as this:
System.out.println(mymonth.JANUARY.FEBUARY.MARCH.APRIL.NOVEMER.SEPTEMBER.JANUARY.JULY.DECEMBER.MAY.FEBUARY.getMonth());
Crazy!!! no? I really can't understand how this is possible (but I
tested it and it really is) How does this work? Why does the program
not crash due to infinite recursion. I'm confused.
//____________________________________________________
public class monthes {
static final monthes JANUARY = new monthes ("January");
static final monthes FEBUARY = new monthes ("Febuary");
static final monthes MARCH = new monthes ("March");
static final monthes APRIL = new monthes ("April");
static final monthes MAY = new monthes ("May");
static final monthes JUNE = new monthes ("June");
static final monthes JULY = new monthes ("July");
static final monthes AUGUST = new monthes ("August");
static final monthes SEPTEMBER = new monthes ("September");
static final monthes OCTOBER = new monthes ("October");
static final monthes NOVEMBER = new monthes ("November");
static final monthes DECEMBER = new monthes ("December");
private String month;
private int numDays;
private monthes(String inmonth) {
if (inmonth.equals("January") || inmonth.equals("March") ||
inmonth.equals("May") || inmonth.equals("July") ||
inmonth.equals("August") || inmonth.equals("October") ||
inmonth.equals("December")) {
numDays = 31;
}
if (inmonth.equals("April") || inmonth.equals("June") ||
inmonth.equals("September") || inmonth.equals("November")) {
numDays = 30;
}
if (inmonth.equals("Febuary")){
numDays = 28;
}
month = inmonth;
}
public String getMonth() {
return(month);
}
public int getNumDays() {
return(numDays);
}
}
}
public class TestMonthes {
public static void main(String[] args) {
monthes mymonth = monthes.AUGUST;
System.out.println(mymonth.JANUARY.FEBUARY.MARCH.APRIL.NOVEMER.SEPTEMBER.JANUARY.JULY.DECEMBER.MAY.FEBUARY.getMonth());
}
}
//____________________________________________________
Thanks for any advice.
Michael