Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Switch Statement: constant expression required at line xx

807603Oct 24 2007 — edited Oct 24 2007
Hey.

I have this method:
private static String convertDayToString(int day) {
        Calendar c = Calendar.getInstance();
        switch (day) {
        case c.SUNDAY:
            return "Sunday";
        case c.MONDAY:
            return "Monday";
        case c.TUESDAY:
            return "Tuesday";
        case c.WEDNESDAY:
            return "Wednesday";
        case c.THURSDAY:
            return "Thursday";
        case c.FRIDAY:
            return "Friday";
        case c.SATURDAY:
            return "Saturday";
        }
        return "UnknownDay";
    }
It should return a String representation of a day based on the passed int argument, gotten via Calendar.DAY, Calendar.DAY_OF_MONTH, Calendar.DAY_OF_YEAR, etc.

Basically, this is what I want it to do:
System.out.println("The current day is: " + convertDayToString(Calendar.getInstance().DAY_OF_MONTH));
-- "The current day is: Wednesday"
I think it is created correctly... but there's a compiler error I've never seen before, and it has to do with Switch Statements, which I don't use all that often.

constant expression required at line xx

It flags the first case line
case c.SUNDAY:
            return "Sunday";
So I changed it to
case 1:
            return "Sunday";
And it flagged the next case line, thus leading me to believe...
Can constants not be used in switch statements?

Edited by: LukeFoss on Oct 24, 2007 9:38 AM

Edited by: LukeFoss on Oct 24, 2007 9:40 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 21 2007
Added on Oct 24 2007
17 comments
3,309 views