We help IT Professionals succeed at work.

Java 5.0 Enum type.

gram77
gram77 asked
on
427 Views
Last Modified: 2008-01-09
I am using  jre1.5.0_10.

When i compile the program below, it generates an error: Class or interface declaration expected.

When i run this code, i get the error:

[root@localhost java]# javac EnumTest.java
EnumTest.java:1: error: Class or interface declaration expected.
public enum Day {
          ^
EnumTest.java:2: error: Class or interface declaration expected.
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
       ^
EnumTest.java:3: error: Class or interface declaration expected.
    THURSDAY, FRIDAY, SATURDAY
       ^
EnumTest.java:4: error: Class or interface declaration expected.
}
   ^
EnumTest.java:7: error: Type ‘Day’ not found in declaration of field ‘day’.
        Day day;
           ^
EnumTest.java:9: error: Type ‘Day’ not found in the declaration of the argument ‘day’ of method ‘<init>’.
        public EnumTest(Day day) {
                           ^
6 errors

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY
}

public class EnumTest {
      Day day;
      
      public EnumTest(Day day) {
            this.day = day;
      }
      
      public void tellItLikeItIs() {
            switch (day) {
                  case MONDAY: System.out.println("Mondays are bad.");
                                   break;
                              
                  case FRIDAY: System.out.println("Fridays are better.");
                                   break;
                                  
                  case SATURDAY:
                  case SUNDAY: System.out.println("Weekends are best.");
                                   break;
                                  
                  default:       System.out.println("Midweek days are so-so.");
                                   break;
            }
      }
      
      public static void main(String[] args) {
            EnumTest firstDay = new EnumTest(Day.MONDAY);
            firstDay.tellItLikeItIs();
            EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
            thirdDay.tellItLikeItIs();
            EnumTest fifthDay = new EnumTest(Day.FRIDAY);
            fifthDay.tellItLikeItIs();
            EnumTest sixthDay = new EnumTest(Day.SATURDAY);
            sixthDay.tellItLikeItIs();
            EnumTest seventhDay = new EnumTest(Day.SUNDAY);
            seventhDay.tellItLikeItIs();
            
            
      }
}



Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
Declare 'Day' *inside* the class
CERTIFIED EXPERT
Top Expert 2016

Commented:
... or in its own compilation unit (file)

Author

Commented:
CEHJ:
Now this is declared inside a class. errors still persist.
class EnumClass
{
  public enum COLORS { RED, BLUE};
}

class UseEnumClass
{
 public static void main(String [] args)
 {
   EnumClass eClass = new EnumClass();

  eClass.COLORS  col1;
 }
}

********************************

[root@localhost java]# javac UseEnumClass.java
UseEnumClass.java:3: error: Invalid declaration.
  public enum COLORS { RED, BLUE};
                        ^
UseEnumClass.java:8: error: Method ‘main’ can't be static in inner class ‘EnumClass$UseEnumClass’. Only members of interfaces and top-level classes can be static.
 public static void main(String [] args)
                       ^
UseEnumClass.java:14: error: '{' expected.
}
   ^
UseEnumClass.java:12: error: Type ‘eClass.COLORS’ not found in the declaration of the local variable ‘col1’.
  eClass.COLORS  col1;
     ^
4 errors

Author

Commented:
CEHJ:
Can't ENUM be declared outside as well inside a class??
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>Can't ENUM be declared outside as well inside a class??

See my second comment. My first comment intended you to use the enum as an inner class of EnumTest

Author

Commented:
CEHJ:
Ok

Author

Commented:
CEHJ;
Can you modify this  code and make it work
class EnumClass
{
  public enum COLORS { RED, BLUE};
}

class UseEnumClass
{
 public static void main(String [] args)
 {
   EnumClass eClass = new EnumClass();

  eClass.COLORS  col1;
 }
}
CERTIFIED EXPERT
Top Expert 2016

Commented:

public class UseEnumClass {

        public enum COLORS { RED, BLUE};

 public static void main(String [] args)
 {
   UseEnumClass eClass = new UseEnumClass();
            System.out.println(COLORS.RED);
 
 }
}

Author

Commented:
CEHJ: I got the following error while running this code.

[root@localhost java]# javac UseEnumClass.java
UseEnumClass.java:3: error: Invalid declaration.
       public enum COLORS { RED, BLUE};
                             ^
UseEnumClass.java:5: error: Illegal modifier ‘public’. Only ‘final’ was expected here.
 public static void main(String [] args)
    ^
UseEnumClass.java:5: error: Illegal modifier ‘static’. Only ‘final’ was expected here.
 public static void main(String [] args)
           ^
3 errors
CERTIFIED EXPERT
Top Expert 2016

Commented:
Paste the last code. It compiles, runs and prints:

RED

Author

Commented:
This is the code i am using:
public class UseEnumClass {

       public enum COLORS { RED, BLUE};

 public static void main(String [] args)
 {
   UseEnumClass eClass = new UseEnumClass();
          System.out.println(COLORS.RED);
 
 }
}

*************************************
Error on compilation:

[root@localhost java]# javac UseEnumClass.java
UseEnumClass.java:3: error: Invalid declaration.
       public enum COLORS { RED, BLUE};
                             ^
UseEnumClass.java:5: error: Illegal modifier ‘public’. Only ‘final’ was expected here.
 public static void main(String [] args)
    ^
UseEnumClass.java:5: error: Illegal modifier ‘static’. Only ‘final’ was expected here.
 public static void main(String [] args)
           ^
3 errors
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
[root@localhost java]# which java
/usr/java/jre1.5.0_10/bin/java
CERTIFIED EXPERT
Top Expert 2016

Commented:
You need to do

which javac

Author

Commented:
CEHJ:
[root@localhost java]# which javac
/usr/bin/javac
keep /usr/java/jre1.5.0_10/bin/ in front of /use/local or compile as
 
/usr/java/jre1.5.0_10/bin/javac UseEnumClass.java
CERTIFIED EXPERT
Top Expert 2016

Commented:
Do

/usr/bin/javac -version

Author

Commented:
CEHJ:

[root@localhost java]# javac -version
Warning: -version not understood. Ignoring.
gcj: no input files

Author

Commented:
CEHJ:
gcj??
It seems it is not pointing to SUN java but gnu version of java.
CERTIFIED EXPERT
Top Expert 2016

Commented:
gcj doesn't support 1.5
CERTIFIED EXPERT
Top Expert 2016

Commented:
(Pretty certain anyway)

Author

Commented:
CEHJ:
I thought that when i set PATH, it will set the path of both javac and java.
So, now how do i set the path for javac??

[root@localhost java]# pwd
/usr/java
[root@localhost java]# ls -l
total 4
drwxr-xr-x 7 root root 4096 Jan 11 07:55 jre1.5.0_10

export PATH=/usr/java/jre1.5.0_10/bin:$PATH
CERTIFIED EXPERT
Top Expert 2016

Commented:
There are two different issues you're getting muddled:

a. compiling Java source with the compiler, javac
b. running Java programs with the runtime, java

Your compiler is out of sync with the runtime. If you want to compile >= 1.5 source, you need to get hold of the appropriate compiler.
CERTIFIED EXPERT
Top Expert 2016

Commented:

Author

Commented:
CEHJ:
I already have downloaded and installed jre1.5.10.
I just want to point javac to the SUN jre not gnu jre.
[root@localhost etc]# which java
/usr/java/jre1.5.0_10/bin/java
CERTIFIED EXPERT
Top Expert 2016

Commented:
The JRE doesn't come with a compiler - you need the JDK

Author

Commented:
Is jre1.5.0_10 ; the version i have on my pc Java6??
Will JDK 6 with Java EE run with jre1.5.0_10?
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>Is jre1.5.0_10 ; the version i have on my pc Java6??

Looks like it. That's a JRE, not a JDK, you need the latter

>>Will JDK 6 with Java EE run with jre1.5.0_10?

It will, but if you've no special reason not to, i'd install any earlier Java

Author

Commented:
CEHJ:
Is this will be compatible with jre1.5.0_10?

http://java.sun.com/javase/downloads/index_jdk5.jsp

JDK 5.0 Update 9 with Java EE

Should i go ahead downloading JDK5.0?

Will i need to set the PATH variable in .bash_profile or .bashrc files once i install JDK5
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>Will i need to set the PATH variable in .bash_profile or .bashrc files once i install JDK5

Yes. I'd define JAVA_HOME too

export JAVA_HOME=xxxxxxxxxxxxxx
export PATH=$JAVA_HOME/bin:$PATH


Why don't you get 6?

CERTIFIED EXPERT
Top Expert 2016

Commented:
JAVA_HOME, incidentally will be the *JDK* install directory

Author

Commented:
I hope there will not be any incompatibility between jre1.5.0_10 and jdk 6
If there will not be any incompatiblity then i will install jdk 6?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> I hope there will not be any incompatibility between jre1.5.0_10 and jdk 6
> If there will not be any incompatiblity then i will install jdk 6?

Yes there will be, I'd suggest installing JDK1.5, *not* 1.6
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>I hope there will not be any incompatibility between jre1.5.0_10 and jdk 6

They are different versions. You will get incompatibility if you try to run classes compiled with 1.6 with 1.5, but it's unclear why you'd want to if you have the ability to run them with 1.6

Author

Commented:
CEHJ:
So in other words i should fresh install jre6 and jdk6 and set the path appropriately.

CEHJ, i am going out of station today and will return in a week's time.

I shall do this installtion then and let you know about the prograss.

Thanks a lot for your  help..
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
do u have a need for java6??  if not then theres no need to install it. Just install the java5 sdk.

Author

Commented:
objects:
Ok i shall install jdk 5.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Not sure why you wouldn't want the latest version, but it's up to you ;-)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.