switch statements and aString.hashCode() constant?

According to everything I have found this should compile but it does not. At one time, there was a compiler that did compile it.

I put a comment in front of the two statements that do not compile.

Any explanation would help.

Thanks,

George

==================================================================

/**

* Title: switch statements and constants?

* Description:Experiment to see if hashCode() can be used as cases for switch statements

* Copyright:Copyright (c) 2003

* @author George Policello

* @version 1.0

*/

public class TestConstantsInSwitchStatements

{ // Directly set a constant (final).

private static final int five = 5;

// Set a constant (final) in two steps.

private static final int tmp01 = 6;

private static final int six= tmp01 ;

// Set a calculated constant (final).

private static final int abcd = "abcd".hashCode() ;

// Set a calculated constant (final) in two steps.

private static final int tmp02 = "efgh".hashCode() ;

private static final int efgh = tmp02 ;

public TestConstantsInSwitchStatements( String string )

{ switch( string.hashCode() )

{case five: System.out.println( "Found:five=<" + string + ">") ; break ;

case six: System.out.println( "Found:six=<" + string + ">") ; break ;

// These next two will not compile. They used to in someones compiler.

case abcd: System.out.println( "Found:abcd=<" + string + ">") ; break ;

case efgh: System.out.println( "Found:efgh=<" + string + ">") ; break ;

default: System.out.println( "Found: default=<" + string + ">") ; break ;

} // End switch()

} // End constructor: TestConstantsInSwitchStatements(String)

public static void main(String[] args)

{ new TestConstantsInSwitchStatements( "abcd" ) ;

new TestConstantsInSwitchStatements( "wxyz" ) ;

} // End method: main(String [])

} // End class: TestConstantsInSwitchStatements(String)

[2093 byte] By [gepii42a] at [2007-9-28 12:51:48]
# 1
In short: The type of the switch expression must be char, byte, short, or int, or a compile-time error occurs, and the case value must be one of the thes types. In detail, see Java Language Spec paragraph 14.10
ChuckBinga at 2007-7-12 8:33:10 > top of Java-index,Developer Tools,Java Compiler...
# 2

I guess it was not clear enough in the code. "abcd" and "efgh" are

static final ints and, hence, match the criteria of the Java Spec. The problem is in the initialization assignments. The two direct assignments for "five" and "six" were meant to demonstrate that the analogous variables not initialized from a call to hashCode() worked correctly.

There is nothing I have seen in the Java Spec. that says one cannot initialized a static final int with a method call and have it usable in a switch statement or any other place requiring a constant that can be converted to an int (any of its smaller siblings or char).

Maybe a longer answer is needed or an expansion would help.

gepii42a at 2007-7-12 8:33:10 > top of Java-index,Developer Tools,Java Compiler...
# 3

The values of "five" and "six" are known at compile time. However, the values of "abcd" and "efgh" are not known at compile time, only at runtime upon class initialization. Although I couldn't see anything in JLS that explicitly stated it, the constant in a case statement must be known at compile time. It probably worked in that "one" compiler at one time because the compiler didn't follow the JLS to the letter.

cknelsena at 2007-7-12 8:33:10 > top of Java-index,Developer Tools,Java Compiler...
# 4
Thanks, that sort of covers it. Disappointing but at least explained.
gepii42a at 2007-7-12 8:33:10 > top of Java-index,Developer Tools,Java Compiler...