aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Switch case Compiler error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Switch case Compiler error" Watch "Switch case Compiler error" New topic
Author

Switch case Compiler error

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Code from Whizlabs..I am getting compile error at line 17 "case expressions must be constant expressions "..Why?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

"case expressions must be constant expressions "..Why?


That's a requirement of the language. Case values must be constants that can be determined at compile time.

Interestingly, Java is not the only language that requires this.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
x4 is not a case expression here ? I see the value of 8 is assigned to x4.
"case expressions must be constant expressions "..Why?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Saibabaa Pragada wrote:x4 is not a case expression here ? I see the value of 8 is assigned to x4.



Integer objects are not defined as a type to be treated as a compile time constant -- so... no.

Henry
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
That means, Auto-unboxing happens at Runtime and not at compile time..Is that statement right ?
Henry Wong wrote:
Saibabaa Pragada wrote:x4 is not a case expression here ? I see the value of 8 is assigned to x4.



Integer objects are not defined as a type to be treated as a compile time constant -- so... no.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Saibabaa Pragada wrote:That means, Auto-unboxing happens at Runtime and not at compile time..Is that statement right ?


Autoboxing is just syntactic sugar. This ...



is the same as this...




As for unboxing, that requires a method call too, which of course, isn't allowed for compile time constants either.

Henry
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Nice explanation.Thank you Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Switch case Compiler error