wood burning stoves 2.0
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes case label int-compatible variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "case label int-compatible variable" Watch "case label int-compatible variable" New topic

case label int-compatible variable

Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 423
howdy friends!

This is a question from the round-up game: http://www.javaranch.com/game/game2.jsp

In a switch statement, the argument to the case label (case: argument) can be any variable which can fit within an int.

Answer: false
The case argument must be either an int literal, or an int-compatible variable which is a constant (i.e. static final)

I agree with the answer, but I think there is one problem in the question and other in the explanation.

It should be (case argument:) and the other problem is when it says ... constant (i.e. static final)

Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
The case arguments must be Compile-time constants
when you mark some variable final and assign it a value , then it is a compile time constant.

It should not be static, it should be Compile time constant and that means it must be final and initialized!!!

static finals are called constants because of some historical reasons..... ( In accordance to Henry Wong )

otherwise final variables are constants.

SCJP 6.0 96%

(Connecting the Dots ....)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Have a look on here, here, here, and here. Go through them, you'll get a clear idea!

Before ask, have a Google search!

|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

yea dude, they must be final.

Imagine if their value changes, the java would lose the reference.

[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
I agree. Here's the link: http://aspose.com/file-tools
subject: case label int-compatible variable
It's not a secret anymore!