Forum : problem while using enum data type outside as well as inside the class
Brief description  about Online courses   join in Online courses
View Gurpreet kohli 's Profile

problem while using enum data type outside as well as inside the class

public class one
{
enum Day
{
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
};

public static void main(String[] args) {
Day yesterday = Day.Thursday;
Day today = Day.Friday;
Day tomorrow = Day.Saturday;

System.out.println("Today is " today);
System.out.println("Tomorrow will be " tomorrow);
System.out.println("Yesterday was " yesterday);
}
}
it throws the foolowing error!
D:\java pgs>javac one.java
one.java:3: ';' expected.
enum Day
^
1 error
and if i use enum data type outside the class then like..
enum Day
{
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
public class one{
public static void main(String[] args) {
Day yesterday = Day.Thursday;
Day today = Day.Friday;
Day tomorrow = Day.Saturday;

System.out.println("Today is " today);
System.out.println("Tomorrow will be " tomorrow);
System.out.println("Yesterday was " yesterday);
}
}
it throws many errors!!
D:\java pgs>javac one.java
one.java:1: Class or interface declaration expected.
enum Day
^
one.java:7: Class Day not found.
Day yesterday = Day.Thursday;
^
one.java:7: Class Day not found.
Day yesterday = Day.Thursday;
^
one.java:7: Undefined variable or class name: Day
Day yesterday = Day.Thursday;
^
one.java:8: Class Day not found.
Day today = Day.Friday;
^
one.java:8: Class Day not found.
Day today = Day.Friday;
^
one.java:8: Undefined variable or class name: Day
Day today = Day.Friday;
^
one.java:9: Class Day not found.
Day tomorrow = Day.Saturday;
^
one.java:9: Class Day not found.
Day tomorrow = Day.Saturday;
^
one.java:9: Undefined variable or class name: Day
Day tomorrow = Day.Saturday;
^
10 errors
please help me debugging this..
Asked by Gurpreet kohli | Apr 13, 2010 |  Reply now
Replies (4)
View gurpreet kohli 's Profile
thanks!! debaspreet..
Apr 23, 2010
View debaspreet chowdhury 's Profile
public class one {
enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,Sunday};
public static void main(String[] args) {

Day yesterday = Day.Thursday;
Day today = Day.Friday;
Day tomorrow = Day.Saturday;

System.out.println("Today is "+today);
System.out.println("Tomorrow will be "+tomorrow);
System.out.println("Yesterday was "+yesterday);

}
}
====================================================================
the above code works...it workd on mine...declared enum Day global to the class one..hence declared it jus b'fo main().
Apr 16, 2010
View gurpreet kohli 's Profile
sory!! teacher its not working and still showing the same 10 errors!!
Apr 16, 2010
View arun kumar das 's Profile
Hi Gurpreet,

I have fixed your program below where enum is outside the class:
________________________________________________________________
enum Day
{
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
public class one{
public static void main(String[] args) {
Day yesterday = Day.Thursday;
Day today = Day.Friday;
Day tomorrow = Day.Saturday;

System.out.println("Today is " +today);
System.out.println("Tomorrow will be " +tomorrow);
System.out.println("Yesterday was " +yesterday);
}
}
__________________________________________________________
The above program works very fine. Please compare the above program with yours and I request you to send me your analysis, this will help you in understanding the concepts better. If you find any difficulty then let me I will give the explanation.

Cheers...
Java Teacher
Apr 15, 2010