Link to home
Start Free TrialLog in
Avatar of gram77
gram77Flag for India

asked on

Java 5.0 Enum type.

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();
            
            
      }
}



Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Declare 'Day' *inside* the class
... or in its own compilation unit (file)
Avatar of gram77

ASKER

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

Avatar of gram77

ASKER

CEHJ:
Can't ENUM be declared outside as well inside a class??
>>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
Avatar of gram77

ASKER

CEHJ:
Ok
Avatar of gram77

ASKER

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;
 }
}

public class UseEnumClass {

        public enum COLORS { RED, BLUE};

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

ASKER

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
Paste the last code. It compiles, runs and prints:

RED
Avatar of gram77

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gram77

ASKER

[root@localhost java]# which java
/usr/java/jre1.5.0_10/bin/java
You need to do

which javac
Avatar of gram77

ASKER

CEHJ:
[root@localhost java]# which javac
/usr/bin/javac
Avatar of Ajay-Singh
Ajay-Singh

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
Do

/usr/bin/javac -version
Avatar of gram77

ASKER

CEHJ:

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

ASKER

CEHJ:
gcj??
It seems it is not pointing to SUN java but gnu version of java.
gcj doesn't support 1.5
(Pretty certain anyway)
Avatar of gram77

ASKER

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
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.
Avatar of gram77

ASKER

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
The JRE doesn't come with a compiler - you need the JDK
Avatar of gram77

ASKER

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?
>>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
Avatar of gram77

ASKER

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
>>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?

JAVA_HOME, incidentally will be the *JDK* install directory
Avatar of gram77

ASKER

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?
> 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
>>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
Avatar of gram77

ASKER

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..
do u have a need for java6??  if not then theres no need to install it. Just install the java5 sdk.
Avatar of gram77

ASKER

objects:
Ok i shall install jdk 5.
Not sure why you wouldn't want the latest version, but it's up to you ;-)