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)

